cont is a String object. "YES" is also a string object. cont == "YES" asks if they are the same object. Sometimes they are, sometimes they aren't, even though the value is the same. You should code it as cont.equals("YES"). This will check if the value is the same. cont == "xxxx" probably should be cont = "xxxx". Hope this helps Mark
Unconfigured Ad Widget
Collapse
Announcement
Collapse
No announcement yet.
Problems using Scanner
Collapse
X
-
Problems using Scanner
Hi everybody! I wonder if someone can tell me why this snippet of code does not work. I have tried both next() and nextLine(), but it just seems like the field cont contains more than just the value (YES) I enter from the command line. Scanner input = new Scanner(System.in); String cont; cont = input.next(); if (cont == "YES") cont == "xxxx"; If I change the filed to be of type int instead of String, and enter 1 from the command line it works. int cont; cont = input.nextInt(); if (cont == 1) cont = 10;
Comment