• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BufferedReader input fails

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
I am taking my first Java programming class, and I am stuck on an unexplained detail with the buffered reader input. I want to take an input from the keyboard, and then use it in an if statement. I can take the screen information and I can return it to the screen without a problem, but if I try to use it for an if statement it just fails. What did they forget to to tell me? Your help would be appreciated. Below is a snippet of the code that returns the offending answer.

import java.io.*;
public class Guessing{
public static void main(String[] args) throws IOException{
//list of variables for user input
String playAnswer = "y", playAnswer2 = "y";
BufferedReader dataIn = new BufferedReader(newInputStreamReader(System.in));
while (playAnswer2 == "y"){
System.out.print("\tPlease enter yes to play a new game, or no to quit the program: ");
playAnswer = dataIn.readLine();
playAnswer2 = playAnswer.substring(0,1);
System.out.println(playAnswer);
System.out.println(playAnswer2);
if (playAnswer2 == "y"){
System.out.println("This is the playAnswer2 == y choice! " + playAnswer2 + " " + playAnswer );
}
else {
System.out.println("This is the playAnswer2 != y choice! " + playAnswer2 + " " + playAnswer );
}
} //end class while (playAnswer2 == "y")
}// end class public static void main(String[] args)
} // end class public class Guessing
This compiles just fine albeit with the wrong return. Any good thoughts would be appreciated. Thanks Bill Boyd
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure if it has anything to do with your problem, but you should create your Reader of System.in with a buffer size of 1 like this:

new BufferedReader(new InputStreamReader(System.in), 1)

I forgot why exactly, but I do remember that the explanation made sense to me at the time
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks as if you're mistakenly using == to compare Strings. In Java, you must use the equals() method. The == operator compares whether the two variables are pointing to the same place in memory, but you want to compare whether the two variables point to Strings with the same content.

If that is the problem, this is not an Intermediate Java issue, it's very much for beginners. If the problem really is more subtle, accept my apologies!
[ November 28, 2005: Message edited by: Peter Chase ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The "==" operator asks if two objects are physically the same chunk of memory, while the "equals()" method generally asks if two objects are "the same". You don't want your "if" statement to test if two objects are actually the same physical object, but rather, you want to test whether two Strings are the same length and contain the same characters in the same order. Therefore, instead of (for example)

if (playAnswer2 == "y") {

you want

if (playAnswer2.equals("y")) {

Note that many people will write this as the equivalent

if ("y".equals(playAnswer2)) {

this avoid some possible errors if playAnswer2 is "null".
 
Bill Boyd
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Java Ranch Memebers,
Thanks for the repies. I was unsure if this was an intermediate problem or not, after reading your replies it seems that it was. My apologies for not being sure about where to post this question. That was the thing that I needed to change however. Next quarter's syllabus says that we will be comparing strings and my textbook (Shelly Cashman series Java Programming) does mention using the equals method in a chapter much later in the book. Again thanks for the guidance.
Bill Boyd
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic