Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

Comparing String Values

I seem to think this should be simple, but it's not... What I'm doing is simple:  I want to compare a user's input (Y/N) to a set value and compare them as an if statement. For example:

sAnswer = Console.readString("Would you like to do it again (Y/N) ? ");

if (sAnswer == "Y") {
    [first thing to do;]
    [second thing to do;]
}
else {
    [first thing to do if not Y;]
    [second thing to do if not Y;]
}

Seems simply enough, but when I try to do that, I will specifically put in "N", and it'll still do the Y statements.  I even put a System.out.println(sAnswer); statement in there, and it tells me exactly what I put (N), which is not Y, but it still continues to do Y anyway.  Is there anything I can do to compare strings??

Jez
0
Jezuit
Asked:
Jezuit
  • 2
1 Solution
 
CEHJCommented:
>>sAnswer == "Y")

should be

sAnswer.equals("Y")

or better

sAnswer.equalsIgnoreCase("Y")
0
 
Tommy BraasCommented:
Or even better still (avoiding null pointers);

"Y".equalsIgnoreCase(sAnswer)
0
 
JezuitAuthor Commented:
damn you were fast :p
0
 
CEHJCommented:
>>Or even better still (avoiding null pointers);

True...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now