Solved

Comparing String Values

Posted on 2003-11-03
4
271 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Title # Comments Views Activity
type mismatch (Object[] to double[] 4 41
Cisco ASA: Java web start no go, asdm launcher no go 3 57
running on tomcat not jboss eap 7.0 3 28
learn programming 8 66
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

740 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