Solved

Comparing String Values

Posted on 2003-11-03
4
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9675219
>>sAnswer == "Y")

should be

sAnswer.equals("Y")

or better

sAnswer.equalsIgnoreCase("Y")
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 9675253
Or even better still (avoiding null pointers);

"Y".equalsIgnoreCase(sAnswer)
0
 

Author Comment

by:Jezuit
ID: 9675295
damn you were fast :p
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9677870
>>Or even better still (avoiding null pointers);

True...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

738 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