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.

Recommended Answers

All 2 Replies

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;
    }
  }
}

Thank you so much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.