问题
Possible Duplicate:
How do I compare strings in Java?
I am having trouble understanding how to use a Java switch statement. After executing a method in one of the case statements, it still then goes to the default statement and runs that too. Here's the code:
Scanner scanner = new Scanner(System.in);
String option = null;
while (option != "5") {
ShowMenu();
option = scanner.nextLine();
switch (option) {
case "1": ViewAllProducts(); break;
case "2": ViewProductDetails(scanner); break;
case "3": DeleteProduct(scanner); break;
case "4": AddProduct(scanner); break;
case "5": break;
default: System.out.println("Invalid option. Please try again."); break;
}
}
The above code is in the main method. After running case "4" for example, it prints "Invalid option."
回答1:
I am modifying your code to re-initialize your scanner reference before reading new option..
Scanner scanner = new Scanner(System.in);
String option = null;
ShowMenu();
option = scanner.nextLine();
while (!"5".equals(option)) {
switch (option) {
case "1": ViewAllProducts(); break;
case "2": ViewProductDetails(scanner); break;
case "3": DeleteProduct(scanner); break;
case "4": AddProduct(scanner); break;
case "5": break;
default: System.out.println("Invalid option. Please try again..."); break;
}
ShowMenu();
scanner = new Scanner(System.in); // Add this here
option = scanner.nextLine(); // Use brand new scanner without any problem..
}
Rest, you can read from the link I provided, to know the difference between various methods for reading user input..
回答2:
while (!option.equals("5")){...}
Use .equals()
for string compare.
==
compare string refrences(Memory location)
.equals()
compare string value
- see here
回答3:
You are comparing Strings with ==
in your while condition (which compares references instead of values - use equals method for value equality), while loop should be changed to following -
while (!option.equals("5"))
It works in C# flawlessly because of operator overloading while operator overloading is not allowed in java (though "+" is overloaded for String and numbers)
回答4:
I think the problem is in the condition of while loop.
Can you change the condition to while(!"5".equals(option))
and then try running the problem.
Hope this helps you.
回答5:
Your scanner.nextLine() should be outside your while. And generally do-while is used better in combination with switch-case.. So, you can better go with it..
A general use of do-while with switch-case, when you are reading from user is like this: -
boolean flag = false;
do {
String option = scanner.nextLine();
switch (option) {
case "1": break;
case "2": break;
default : flag = true;
break;
}
} while(! flag);
And one important thing to know is: - Strings are Immutable in Java..
Generally for String comparisons in Java, you should use equals() function if you want to compare their values.. And also, I suggest you to override this equals() and hashcode() methods in your class (In future if you make one), to compare two instances of your class..
Actually, there is a big difference between == and equals(). It actually depends on how you are creating your string.. For E.g., suppose this is your code: -
String a = "5";
String b = new String("5");
String c = "5"; or String c = a;
System.out.println("a == b?" + (a == b)); // Will Print false
System.out.println("a.equals(b)?" + a.equals(b)); // Will Print true..
System.out.println("a == c?" + (a == c)); // Will Print true
System.out.println("a.equals(c)?" + a.equals(c)); // Will Print true..
In the above code, your String references 'a' and 'c' are pointing to the same object represented by "5". Whereas the reference "b" is pointing to a different object "5", because using "new" operator to create a String always creates a new Object..
Now, == operator checks whether the two String reference being compared points to the same object or different, whereas equals() function compares the actual value contained by the two string objects..
So, the output in the program will be clear now.. I just explained this because, in your while, you will have to use equals() function to check whether your "option" reference has the same value as "5".
Now, I think you can change your code a little bit, and preferably use do-while to get required output..
回答6:
I stopped passing scanner to each function and created a new one inside each function instead. That fixed the problem. So, I figured out that problem.. but, I want to know why..
If you posted an SSCE, we could probably answer that. Unfortunately, the behaviour you are describing involves code that you haven't shown us.
is this a bug in Java or normal?
It is highly unlikely that this is a Java bug. The Scanner
code has been in Java for a few years, and any significant bugs are likely to have been encountered before and fixed.
On the other hand, I can think of no reason why passing a Scanner
to a method would make any difference per se.
But the bottom line is that without an SSCE it is not possible to figure out what your code is really doing ... and explain the root cause.
If I can understand why this is a problem then I can avoid it in future with other classes too.. unless this only happens with Scanner?
Without an SSCE we can't help you understand.
来源:https://stackoverflow.com/questions/12542352/java-how-to-use-a-switch-statement