Java Buffer Reader

 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 you type "x", the loop will not exit.

+3
source share
3 answers

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

+5
source

Beware of “standard” comparison operators when working with strings.

str != "x"

compares two links, not the contents of a string. Use the "equals" method to compare the contents of a string.

+5
source

You have to check equals()

+1
source

Source: https://habr.com/ru/post/1764836/


All Articles