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
JezuitAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
>>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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.