0

Hey guys im making a simple program. I have a while loop that continues while the bool quit ! = true. I am trying to test that in a statement like this:

String userdecision = "";

while (quit!=true){ //start while

 System.out.print("Would you like to continue with another transaction?");
        
[B]userdecision=in.next();[/B]

        if (userdecision=="no"){
            quit = true;
            
        }

} //end while loop

There is something going wrong. I think it's the part i put in bold. I don't think i have the right syntax for getting the users input and then breaking out of the program. I have all the java utils imported into the project and i am using scanner to get the input from the user. Can anybody help me with the right syntax for this. THanks.

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by jhonnyboy
0

Simply run an infinite while loop, wait for user input, process that input and make your decision accordingly.

// create Scanner etc.
while(true) {
  if(in.hasNext()) {
    String str = in.next();
    if(str == null || str.trim().equals("no")) {
      System.out.println("Thank you");
      System.exit(0); // or break;
    }
  }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.