Google Answers Logo
View Question
 
Q: Testing a String Input in Java (very easy) ( No Answer,   0 Comments )
Question  
Subject: Testing a String Input in Java (very easy)
Category: Computers > Programming
Asked by: tamkins-ga
List Price: $5.00
Posted: 25 Feb 2004 16:34 PST
Expires: 25 Feb 2004 16:50 PST
Question ID: 310827
Why doesn't this program work?  Specifically, you can answer yes, the
'Debug' line confirms that answer is "yes", but you still get the
third message.  I tried using a StringTokenizer as well, and that
didn't help.  Here is the program:

import java.io.*;

public class AnswerTest {
   public static void main (String[] args) throws Exception {
      System.out.println("Please enter yes or no.");

      BufferedReader br = 
         new BufferedReader(new InputStreamReader(System.in));
      String answer = br.readLine();

      System.out.println("Debug: answer = \"" + answer + "\".");

      if (answer == "yes" || answer == "Yes") 
         System.out.println("You said yes.");
      else if (answer == "no" || answer == "No")
         System.out.println("You said no.");
      else
         System.out.println("You said something else.");
   }
}
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy