1

When ever I run this the output is:

"Please input your name" I enter it

"Please enter student number" I enter it

"Please input the student name"

"please enter the student number"

String nameChoice = "";

do{

        System.out.println("Please input the students name");

        String name = s.nextLine();
        nameChoice = name;

        System.out.println("Please enter that students number");
        int studentNo = s.nextInt();



}while(!nameChoice.equalsIgnoreCase("done"));
ceri Westcott
  • 45
  • 2
  • 8

1 Answers1

1

This is because s.nextInt() reads only the integer entered and not the newline character after the entered integer. So you can add a s.nextLine() to read that newline character.

So do this:

int studentNo = s.nextInt();
s.nextLine();

If you are not doing this the next nextLine() method will be used to read the newline you entered.

Another method to avoid this problem is reading using nextLine() and parsing it to integer.

int studentNo = Integer.parseInt(s.nextLine()); 
Johny
  • 2,003
  • 3
  • 17
  • 31