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..

Vincent Dagpin
  • 3,581
  • 13
  • 55
  • 85

3 Answers3

5

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

helios
  • 13,574
  • 2
  • 45
  • 55
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.

vulkanino
  • 9,074
  • 7
  • 44
  • 71
1

You must check for equals()

Dheeraj Joshi
  • 3,057
  • 8
  • 38
  • 55