Author | Message | Time |
---|---|---|
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. [code]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"); } } } }[/code] | October 13, 2006, 2:49 AM |
HdxBmx27 | 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. [code]if (args[0].substring(1,1).equals("-"))[/code] ~-~(HDX)~-~ | October 13, 2006, 8:27 AM |
Myndfyr | [quote author=HdxBmx27 link=topic=15866.msg159772#msg159772 date=1160728026] 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. [code]if (args[0].substring(1,1).equals("-"))[/code] ~-~(HDX)~-~ [/quote] 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. | October 13, 2006, 8:36 AM |
HdxBmx27 | Bah ya, sorry it was 1am >.< Anyways, my point was clear wasn't it? Strings are objects not a basic data type. ~-~(HDX)~-~ | October 13, 2006, 2:39 PM |
Brandon | [quote author=FrostWraith link=topic=15866.msg159758#msg159758 date=1160707743] 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. [code]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"); } } } }[/code] [/quote] 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: [code] 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"); } } } } [/code] It's a tad easier to read. You don't have to, but I think it's easier to read and check for errors. | October 13, 2006, 9:56 PM |
FrostWraith | Thx for ur help Hdx. And thx for the syntax tip. I kinda really never knew which way I wanted to do it. | October 14, 2006, 3:16 AM |
HdxBmx27 | 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)~-~ | October 14, 2006, 3:19 AM |
Myndfyr | [quote author=AntiVirus link=topic=15866.msg159785#msg159785 date=1160776595] It's a tad easier to read. You don't have to, but I think it's easier to read and check for errors. [/quote] 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. | October 14, 2006, 10:04 AM |
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 [code]public static void main(String[] args)[/code] line means :( | October 15, 2006, 8:24 PM |
HdxBmx27 | 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 [code]public static void main(String[] args)[/code] 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)~-~ | October 15, 2006, 8:55 PM |