0

It works (not as I wanted, the loop breaks if there is any 'x' present in the text like 'asdx') if I set condition as c=='x' but not if I put it as s=="x" (so loop breaks only if I type a single x only)

public static void main(String[] args) throws IOException {
    InputStreamReader i = new InputStreamReader(System.in);
    String z = "";
    boolean bool=true;
    while (bool==true) {
        int x = i.read();
        char c = (char) x;
        String s = Character.toString(c);
        z += s;
        if (s=="x") {
            bool=false;
        }
    }
    out.println(z);
}
James Dunn
  • 8,064
  • 13
  • 53
  • 87
Het
  • 203
  • 1
  • 3
  • 15

1 Answers1

1

use equals to compare String values:

if(s.equals("x")){
  bool=false;
}

with == you compare the object references.

nano_nano
  • 12,351
  • 8
  • 55
  • 83