3
 import java.io.*;

 public class TerminateWhen
 {
   public static void main(String args[]) throws IOException
   {
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

     String str = "";
     System.out.println("Type \"x\" to exit..");
     do {
       str = br.readLine();
       System.out.println(str);
       }
     while(str!="x");
   }
 }

the problem is even if will type the "x", the loop will not exit..

1

3 Answers 3

5

Try !str.equals("x") !!!

Sign up to request clarification or add additional context in comments.

Comments

5

Beware "standard" comparison operators when you're working with strings.

str != "x"

compares the two references, not the string's contents. Use the "equals" method to compare the string contents.

Comments

1

You must check for equals()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.