None of these end up as true when I enter "yes", "y", or "Y". I've tried entering them with and without quotes. What am I doing wrong?
System.out.println("yes/no");
//get answer
getDirections = in.nextLine();
if (getDirections == "yes"){
game.printDirections();
}
if (getDirections == "y"){
game.printDirections();
}
if (getDirections == "Y"){
game.printDirections();
}
Name:
Anonymous2007-01-18 22:16
maybe in.nextLine() is getting the line with a line break (\n) after it? i haven't done much java this year, but you might have more luck with (getDirections == "y\n") or using a different input function.
In Javur, objectA==objectB is true if they are the exact same object, not if they're different objects with the same contents. What you want to do requires the equals method: getDirections.equals("yes").
Name:
Anonymous2007-01-18 22:36
Thanks! ^_^
Name:
Anonymous2007-01-18 22:38
getDirections.equalsIgnoreCase("yes"); may help too. i mean, if the user enteres YEs or yEs or something!
Name:
Anonymous2007-01-18 22:39
string literals in java can't use boolean operators like != , or == . gotta do stupid shit like .equals(), .equalsIgnoreCase(), !(.equalsIgnoreCase()) etc. etc.!
Name:
Anonymous2007-01-19 5:15
Three comments:
1. Java fails for not being able to overload operators (because shit.equals(crap) is so much clearer and more maintainable that shit == crap, not to mention the inherent symmetry of == is not reflected, and thanks for making my objects less useful than built-in objects, oh, wait, int is not an object? Lol, what a failure of a language).
2. Java would fail even if it supported operator overloading.
3. I hate how Berkeley hippies and similar people fuck you in the ass when you want to do anything caseless. "equalsIgnoreCase"? My god, I bet they chose that name on purpose, to piss off people who prefers offering a decent, caseless behaviour to their users. If I made that API, just to piss hippies off, caseless string comparison would be eq, and case-sensitive comparison would be equalsCompareUsingCaseSensitiveMatching.
>>7
It wouldn't really bother me if it was actually consistent. The stupid thing is, you can do something like stringA+stringB to concatenate two strings (in fact, if you do this with objects, it automatically invokes toString).
Name:
Anonymous2007-01-19 14:48
if (getDirections.compareTo("Yes") == 0 || getDirections.compareTo("y") == 0 || getDirections.compareTo("Y") == 0) {
//code
}
et al.