• Welcome to Valhalla Legends Archive.
 

I got some beginner trouble

Started by FrostWraith, October 12, 2006, 09:49 PM

Previous topic - Next topic

FrostWraith

OK. This snippet isn;t actually what I plan to do but I need to know how to do this. I cannot see why this code doesn't function properrly.

public class test {
    public static void main(String[] args){
if (args.length > 0){
    if (args[0].substring(1,1) == "-"){
        System.out.println("The first letter of your first argument was a hyphen");
    }
}   

    }
}

Hdx

you can't compare strings like that.
Strings in java are basically byte array objects.
so String1 == String2 would compare the pointer of String1 to the pointer of String2. Not the actual data.
BUT, ya, jsut use the .equals() function.
if (args[0].substring(1,1).equals("-"))
~-~(HDX)~-~

Proud host of the JBLS server www.JBLS.org.
JBLS.org Status:
JBLS/BNLS Server Status

MyndFyre

Quote from: HdxBmx27 on October 13, 2006, 03:27 AM
you can't compare strings like that.
Strings in java are basically byte array objects.
so String1 == String2 would compare the pointer of String1 to the pointer of String2. Not the actual data.
BUT, ya, jsut use the .equals() function.
if (args[0].substring(1,1).equals("-"))
~-~(HDX)~-~
They're really not basically byte arrays, they're much closer to char arrays, and they have a lot of specialized processing to support localization/globalization.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Hdx

Bah ya, sorry it was 1am >.<
Anyways, my point was clear wasn't it?
Strings are objects not a basic data type.
~-~(HDX)~-~

Proud host of the JBLS server www.JBLS.org.
JBLS.org Status:
JBLS/BNLS Server Status

AntiVirus

#4
Quote from: FrostWraith on October 12, 2006, 09:49 PM
OK. This snippet isn;t actually what I plan to do but I need to know how to do this. I cannot see why this code doesn't function properrly.

public class test {
    public static void main(String[] args){
if (args.length > 0){
    if (args[0].substring(1,1) == "-"){
        System.out.println("The first letter of your first argument was a hyphen");
    }
}   

    }
}

Since the question was already answered, I am not going to try to explain that.  But as a helpful suggestion, I suggest you don't put { at the end of a line, but instead put it under the line.

For example, it would help if your code looked like this:

  public class test
  {
      public static void main(String[] args)
      {
if (args.length > 0){
     if (args[0].substring(1,1) == "-")
             {
         System.out.println("The first letter of your first argument was a hyphen");
     }
       }   

   }
}

It's a tad easier to read.  You don't have to, but I think it's easier to read and check for errors.
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

FrostWraith

Thx for ur help Hdx. And thx for the syntax tip. I kinda really never knew which way I wanted to do it.

Hdx

Meh here is sjut a tip, go with whatever syntax you like the most.
Some like it the way AV said, I myself prefer the way you had it int he 1st place.
Just go with whatever you feel most comfertable with, its all personal preferance.
~-~(HDX)~-~

Proud host of the JBLS server www.JBLS.org.
JBLS.org Status:
JBLS/BNLS Server Status

MyndFyre

Quote from: AntiVirus on October 13, 2006, 04:56 PM
It's a tad easier to read.  You don't have to, but I think it's easier to read and check for errors.
While I agree and prefer this syntax, there are people who prefer it the other way.  Most modern IDEs support user preference for automatic formatting with either option.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

nahud

A little off topic but... how come schools teach Scanner class before that "console input way" (Not sure what it's really called). Is it for simplicity? I'm currently taking Introduction to Programming in Java and I do find that Scanner class makes things a lot easier/shorter than the other way. Maybe it's because I'm in the intro class... my teacher hasn't explained what

public static void main(String[] args)

line means :(

Hdx

What do you mean by scanner class?
Something that you use to get input form the user?
Like: int input = Scanner.getInt();
If so then its just a black box object for user input, cuz your teacher dosen't want to teach you how to get the input yourself.

As for public static void main(String[] args)
Every program you create must have a static entry point. Java looks for a function called "main" in the class you want to run.
I assume you know how to build a function signature... so you should know what everything else on that line means...
~-~(HDX)~-~

Proud host of the JBLS server www.JBLS.org.
JBLS.org Status:
JBLS/BNLS Server Status