Java: switch statement bug.. or normal behaviour?
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java">How do I compare strings in Java?</a>  </p>
</blockquote>



<p>I'm coming from a C# background and learning Java. What I am about to describe is rather weird behaviour from my point of view.. but then maybe this is normal for Java..</p>

<p>I am using a switch block and 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:</p>

<pre><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;
            }
        }
</code></pre>

<p>The above code is in the main method. After running case "4" for example, it will then print "Invalid option..." which is from the default case... is this normal behaviour in java?!? Maybe I'm just doing something very stupid or noobish.. I don't know.. but all the break statements are there, so I don't get it..</p>

<p><strong>EDIT</strong></p>

<p>Err.. guys, I marked as answer too soon. I changed my code but it's still not working. Here's my code now:</p>

<pre><code>Scanner scanner = new Scanner(System.in);
        String option = null;

        while (!"5".equals(option)) {
            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;
            }
        }
</code></pre>

<p>I restarted and rebuilt the app, but I still see the "Invalid option..." message being displayed after running a method in one of the case stataments. I'm using NetBeans 7.2</p>

<p><strong>EDIT 2</strong></p>

<p>Latest code:</p>

<pre><code>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();
            option = scanner.nextLine();
        }
</code></pre>

<p>What I am seeing now:</p>

<ol>
<li>Select option #4.. add a product to sql table..</li>
<li>When it comes out of AddProduct function, it runs ShowMenu().. but then it compleetly ignores the next line with <code>option = scanner.nextLine();</code></li>
<li>So, it goes back to the top of the loop again and now option is an empty string and that's why it prints "Invalid option".</li>
<li>But then strangely this second time it will actually go to the line <code>option = scanner.nextLine();</code> and set the value that I give it.</li>
</ol>

<p>I think I know what's causing it, but I don't know why... I tested with ALL the cases.. and with case #1 it works fine.. I don't have a problem. What's different? I'm not passing a "scanner" to it. So obviously that's causing a problem. But why?? Why does it ignore the line <code>option = scanner.nextLine();</code> ? For me that doesn't make sense. Please someone explain nicely.</p>

<p><strong>EDIT 3</strong></p>

<p>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.. is this a bug in Java or normal? 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?</p>
