0

The code doesn't exit after I type "stop" - for some reason. Why? Step-by-step debugging shows that after I enter "stop" it's value consists of exactly 's','t','o','p' without any line breaks, etc. - however, the code still goesn't exit. Could anyone tell why, please?

import java.util.Scanner;

public class Application {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    // asking username
    System.out.print("Username: ");
    String username = input.nextLine();

    String inpText;
    do {
        System.out.print(username + "$: ");
        inpText = input.nextLine();
        System.out.print("\n");
        // analyzing
        switch (inpText) {
        case "start":
            System.out.println("> Machine started!");
            break;
        case "stop":
            System.out.println("> Stopped!");
            break;
        default:
            System.out.println("> Command not recognized");
        }
    } while (inpText != "stop");

    System.out.println("Bye!..");
}
}
evictorov
  • 33
  • 1
  • 12
  • 1
    read this: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) and then fix your `while` condition. – jlordo Aug 02 '13 at 10:27
  • Thanks @jlordo. Couldn't imagine the reason was in the comparison itself; and the linked question covers everything. – evictorov Aug 02 '13 at 14:20

4 Answers4

2
  • To compare Strings use .equals() and not ==, unless you really know what you are doing.
inpText != "stop" //Not recommended
!"stop".equals(inpText) //recommended

Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted

Community
  • 1
  • 1
rocketboy
  • 9,573
  • 2
  • 34
  • 36
  • Thanks @rocketboy. JDK is 7u17, and looks like it was a matter of correct comparison. However, acc. to specification of the `equals()` shouldn't `!inpText.equals("stop")` be more correct? – evictorov Aug 02 '13 at 14:01
  • 1
    String equals is symmetric, so `a.equals(b)` is same as `b.equals(a)`. That said, if `inpText = null`, `inpText.equals("stop")` can throw a NPE but it won't be thrown for `"stop".equals(inpText)` – rocketboy Aug 02 '13 at 16:40
0

You are comparing pointers not strings with this piece of code:

while (inpText != "stop");

Should be something like this:

while (!"stop".equals(inpText));
Tristan Van Poucke
  • 323
  • 1
  • 3
  • 14
  • Thanks Tristan. However, according to the function' specification shouldn't !inpText.equals("stop") be more correct? – evictorov Aug 02 '13 at 14:04
  • evictorov: by writing it as !"stop".equals(inpText) you easily avoid any possible nullpointers. Its just a little trick I use quite often. – Tristan Van Poucke Aug 04 '13 at 16:00
0

change while (inpText != "stop"); to while (!(inpText.equals("stop")));

0

If your JDK is 1.6 or lower you can't switch() on a String

P.S. switching on a String is probably not the best solution Yeah in java 1.6 you can only switch int, boolean, double, long, and float I believe.

Mohsin Shaikh
  • 494
  • 1
  • 4
  • 11