0

I am trying to create a program that asks the user for a student's name and asks him if the user would like to enter another user (Y/N). This program is repeated until the user enters N after a student is entered.

    public static void main(String[] args) {
    String student;
    char choice;
    Scanner s =new Scanner (System.in);

    System.out.println("Enter a student");
    student = s.nextLine();
    System.out.println("Do you want to enter an other student? (Y/N)");
    choice = s.next().charAt(0);

    switch(choice){
        case 'Y':
            do{
                System.out.println("Enter an other student");
                student = s.nextLine();
                System.out.println("Do you want to enter an other student? (Y/N)");
                choice = s.next().charAt(0);
            }while (choice == 'N');
            System.out.println("End");
            break;
        case 'N':
            System.out.println("End");
            break;
    }

}

In the do loop, the program doesn't wait for the user to enter another student. It justs ask the first question and then asks the second without waiting.

  • 1
    https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo Use BufferedReader – denvercoder9 Nov 20 '19 at 10:46
  • Your `while` condition should be negated - keep asking while the choice remains 'yes' – Thomas Timbul Nov 20 '19 at 10:59
  • 1
    Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Till Schäfer Nov 20 '19 at 11:26

3 Answers3

1

The code before while loop only for the reason to have a different question for the first time. Other than that I would do it as Eugene Borisov suggested.

public static void main(String[] args) {

    String student, choice;
    Scanner input = new Scanner (System.in);
    System.out.println("Enter a student");
    student = input.nextLine();
    System.out.println("Do you want to enter an other student? (Y/N)");
    choice = input.nextLine();

    while(choice.toLowerCase().equals("y")) {
        System.out.println("Enter a student");
        student = input.nextLine();
        System.out.println("Do you want to enter an other student? (Y/N)");
        choice = input.nextLine();
    }
    System.out.println("End");

}
projectone
  • 11
  • 3
0

Using next() leaves a newline character in the buffer and the nextLine() reads it.

Replace

choice = s.next().charAt(0);

with

choice = s.nextLine().charAt(0);

and change the condition in while to :-

while (choice != 'N');

Somil Garg
  • 474
  • 4
  • 12
0

Can't understand ur logic. But hope that it could be helpful:

public static void main(String[] args) {
    String student;
    char choice;
    Scanner s = new Scanner(System.in);
    do {
        System.out.println("Enter a student");
        student = s.next();
        System.out.println("Do you want to enter an other student? (Y/N)");
        choice = s.next().charAt(0);
    } while (choice != 'N');

}