0

How many guests will there be there? 4

Enter the choice for guest 1

Enter the choice for guest 2


After I enter the number of guests, guest 1 and 2 fall right under each other, without giving me the chance to input the choice for guest 1, it goes to guest 2. how can I fix this?

public static void main(String[] args) {
        Scanner simpleInput= new Scanner (System.in);

        int guest;
        String choice;
        System.out.println("Welcome to Tying the Knot Wedding Planner Guide");
        System.out.println();
        System.out.println("For each guest, please enter he/her choice for dinner: Beef, Chicken, Fish, or\r\n" + 
                "Vegetarian.\r\n" + 
                "");
        System.out.print("How many guests will there be there? ");
        guest=simpleInput.nextInt();

        while (guest <0 )
        {
            System.out.println ( "That was not a valid number, try again. " );
            System.out.println("How many guests will there be there? ");
            guest=simpleInput.nextInt();
        }

        for (int x = 1; x < guest; x++ )
        {
        System.out.println("Enter the choice for guest  " + x);
        choice=simpleInput.nextLine();
        }

        System.out.println("There will be " + guest + " meals");
    }
Manjunath H M
  • 578
  • 1
  • 5
  • 16

2 Answers2

0

please change your code from

guest=simpleInput.nextInt();

to

guest=Integer.parseInt(simpleInput.nextLine());

This will help you to solve your problem

Manjunath H M
  • 578
  • 1
  • 5
  • 16
0

When you use the method .nextInt() the scanner reads and parses the text until the new line character (\n), therefore when trying to use any read methods again the scanner is not empty (it has the \n character) this is why it assumes the user did input another string You can overcome this in many ways, one is to simply remove the /n from the scanner by putting one more line exactly after the .nextInt()

simpleInput.nextline();

Other way is to get the whole line then to parse the int out of it, leaving no /n character

guests = Integer.parseInt(simpleInput.nextLine());