Josh
Josh

Reputation: 163

Switch in Java is looping

My switch statement just keeps looping. It should print the option you choose and then reprint the menu. Please help! Here is my code:

menu ="\n\t1  Create Account" + 
            "\n\t2  Check balance" +
            "\n\t3  Withdraw" +
            "\n\t1  Deposit" + 
            "\n\t2  Get account ID" +
            "\n\t3  Set ID" +
            "\n\t1  Display Account Info" +

            "\n\t0  Quit\n\n\n";

    System.out.println(menu);
    System.out.println("\tEnter your selection:  ");
    option = scan.nextInt();

while (option != 0) {

            switch (option) {

            case 1: //  Enter and Validate SSN
                        System.out.print("option 1");
                        break;

            case 2:     //Enter and Validate Passwords
                        System.out.print("option 2");
                        break;

            case 3:     //Enter,Verify, and Translate a Phone keypad Number
                        System.out.print("option 3");
                        break;

            case 4: //  Enter and Validate SSN
                        System.out.print("option 4");
                        break;

            case 5:     //Enter and Validate Passwords
                        System.out.print("option 5");
                        break;

            case 6:     //Enter,Verify, and Translate a Phone keypad Number
                        System.out.print("option 6");
                        break;

            case 7:     //Enter,Verify, and Translate a Phone keypad Number
                        System.out.print("option 7");
                        break;

            default: outputString = "\nInvalid Selection\n";
                        System.out.println(outputString);
                        break;

        }
    }

Upvotes: 1

Views: 3370

Answers (4)

Psyrus
Psyrus

Reputation: 1321

A do while loop would require less code:

menu ="\n\t1  Create Account" + 
        "\n\t2  Check balance" +
        "\n\t3  Withdraw" +
        "\n\t1  Deposit" + 
        "\n\t2  Get account ID" +
        "\n\t3  Set ID" +
        "\n\t1  Display Account Info" +

        "\n\t0  Quit\n\n\n";

do {
  System.out.println(menu);
  System.out.println("\tEnter your selection:  ");
  option = scan.nextInt();
  switch (option) {

    case 1: //  Enter and Validate SSN
    System.out.print("option 1");
    break;

    case 2:     //Enter and Validate Passwords
    System.out.print("option 2");
    break;

    case 3:     //Enter,Verify, and Translate a Phone keypad Number
    System.out.print("option 3");
    break;

    case 4: //  Enter and Validate SSN
    System.out.print("option 4");
    break;

    case 5:     //Enter and Validate Passwords
    System.out.print("option 5");
    break;

    case 6:     //Enter,Verify, and Translate a Phone keypad Number
    System.out.print("option 6");
    break;

    case 7:     //Enter,Verify, and Translate a Phone keypad Number
    System.out.print("option 7");
    break;

    default: outputString = "\nInvalid Selection\n";
    System.out.println(outputString);
    break;

  } while (option != 0)


}

Upvotes: 1

fastcodejava
fastcodejava

Reputation: 41127

Your code keeps on looping because value of option never changes when you are in the loop. You need to write your logic differently. Btw, switch does not loop, while does the looping.

Upvotes: 0

Shawn
Shawn

Reputation: 7365

Option is never changing in the while loop you have -- resulting in an endless loop if you type in anything that's not 0

Upvotes: 2

Kirk Woll
Kirk Woll

Reputation: 77616

I'm pretty sure it's your while loop that is doing the looping. And you are never changing the value of option within the body of the loop, so of course it runs continuously.

Presumably, you want to move the line:

option = scan.nextInt();

To the first line of the loop:

while (option != 0) {
    option = scan.nextInt();
    ... 
}

Upvotes: 8

Related Questions