Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

String with Relational Operators in Java?

Name: Anonymous 2007-01-18 21:52

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: Anonymous 2007-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.

Name: Anonymous 2007-01-18 22:25 (sage)

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: Anonymous 2007-01-18 22:36

Thanks! ^_^

Name: Anonymous 2007-01-18 22:38

getDirections.equalsIgnoreCase("yes"); may help too. i mean, if the user enteres YEs or yEs or something!

Name: Anonymous 2007-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: Anonymous 2007-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.

Name: Anonymous 2007-01-19 10:25 (sage)

>>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: Anonymous 2007-01-19 14:48

if (getDirections.compareTo("Yes") == 0 || getDirections.compareTo("y") == 0 || getDirections.compareTo("Y") == 0) {
    //code
}
et al.

Name: Anonymous 2007-01-19 16:53 (sage)

if(Character.toLowerCase(getDirections.charAt(0))=='y') doStuff();

Name: Anonymous 2009-01-14 13:39

WHBTC

Name: Anonymous 2009-03-06 7:12

Template struct FizzBuzz 0.

Name: Trollbot9000 2009-07-01 10:06

Contents name substr f.

Name: Anonymous 2010-11-28 0:21

Name: Anonymous 2010-12-10 7:34

Name: Anonymous 2010-12-22 5:51

Name: Anonymous 2011-02-04 16:04

Name: Anonymous 2011-02-04 18:11

Name: Sgt.Kabu�Ѩkiman业楃 2012-05-28 21:50

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List