user3629146
user3629146

Reputation: 107

Switch statement loops through

I'm trying to make a simple Menu with the switch statement. However i'm having a problem with the switch:

public class main {

    public static void main(String[] args) throws IOException {

         printMenu();

    }

    public static void printMenu() throws IOException{

    char selection = 0;
    do{
        System.out.println("Choose option: ");
        System.out.println("1. Option 1");
        System.out.println("2. Option 2");
        System.out.println("3. QUIT");
        System.out.println("\t\t\t");

        selection = (char)System.in.read();

        switch(selection){          
            case '1':

                    System.out.printf("opt1 chosen\n");

                break;

            case '2':

                System.out.printf("opt2 chosen\n");

                break;                  

            case '3':

                break;

            }
        }
        while(selection != '3'); 

    } 
}

For some reason, when selecting either one or two, the result is that print menu gets printed twice, like this:

Program output:

Choose option: 
1. opt1.
2. opt2.
3. opt3.

1
opt1 chosen
Choose option: 
1. opt1.
2. opt2.
3. opt3.

Choose option: 
1. opt1.
2. opt2.
3. opt3.

The question is, what causes this problem?

Upvotes: 2

Views: 93

Answers (3)

Bhoot
Bhoot

Reputation: 2643

As Peter pointed out, the problem arises because of the way you are reading the 'selection' input. You can correct the functionality as follows:

public class main {

    public static void main(String[] args) throws IOException {
        printMenu();
    }

    public static void printMenu() throws IOException {
        char selection = '0';
        while (selection != '3') {
            if (selection != '\n') {
                System.out.println("Choose option: ");
                System.out.println("1. Option 1");
                System.out.println("2. Option 2");
                System.out.println("3. QUIT");
                System.out.println("\t\t\t");
            }
            selection = (char) System.in.read();
            switch (selection) {
                case '1':
                    System.out.printf("opt1 chosen\n");
                    break;
                case '2':
                    System.out.printf("opt2 chosen\n");
                    break;
                case '3':
                    break;
                default:
                    break;
            }
        }
    }
}

Upvotes: 1

Vyncent
Vyncent

Reputation: 1205

Peter Lawrey is right

I'm suggest using Scanner class :

public static void printMenu() throws IOException {
    Scanner scanner = new Scanner(System.in);
    int selection = 0;
    do{
        System.out.println("Choose option: ");
        System.out.println("1. Option 1");
        System.out.println("2. Option 2");
        System.out.println("3. QUIT");
        System.out.println("\t\t\t");

        selection = (char) scanner.nextInt();

        switch(selection){
        case 1:

            System.out.printf("opt1 chosen\n");

            break;

        case 2:

            System.out.printf("opt2 chosen\n");

            break;

        case 3:

            break;

        }
        scanner.nextLine();

    }
    while(selection != '3');

}

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533920

When you press a number and <Enter> this is two characters not one. i.e. you are typing

1\n

This is unavoidable, but you can chose to parse the input differently with Scanner which handles this differently, or you can ignore it. (or you can expect the user must type a \n after a number...

Upvotes: 9

Related Questions