Solved

Comparing String Values

Posted on 2003-11-03
4
243 Views
Last Modified: 2010-03-31
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
Comment
Question by:Jezuit
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
Comment Utility
>>sAnswer == "Y")

should be

sAnswer.equals("Y")

or better

sAnswer.equalsIgnoreCase("Y")
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
Or even better still (avoiding null pointers);

"Y".equalsIgnoreCase(sAnswer)
0
 

Author Comment

by:Jezuit
Comment Utility
damn you were fast :p
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Or even better still (avoiding null pointers);

True...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now