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!..");
}
}
2
  • 1
    read this: How do I compare strings in Java? and then fix your while condition. Commented Aug 2, 2013 at 10:27
  • Thanks @jlordo. Couldn't imagine the reason was in the comparison itself; and the linked question covers everything. Commented Aug 2, 2013 at 14:20

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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)
0

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

while (inpText != "stop");

Should be something like this:

while (!"stop".equals(inpText));

2 Comments

Thanks Tristan. However, according to the function' specification shouldn't !inpText.equals("stop") be more correct?
evictorov: by writing it as !"stop".equals(inpText) you easily avoid any possible nullpointers. Its just a little trick I use quite often.
0

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

Comments

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.

1 Comment

Thanks Mohsin. Mine is 7u17.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.