0

Im using nextLine to get the student name and nextInt to get the Student Id, I have researched on this and tried using Integer.parseInt(input.nextLine()); , but no luck. its working fine the first time but when it loops back to add in another student it asks for the student name and id in the same line which gives input mismatch error. Any solution to this without keeping input.nextLine() before asking for id as this will force the user to press enter twice after inputting the student name in the first loop. Thanks in advance

String input = "no";
do {
    System.out.print("Enter Student Name: ");
    String na = in.nextLine();

    System.out.print("Enter Student ID: ");
    int id = Integer.parseInt(in.nextLine());

    System.out.print("Enter Excercise grade: ");
    double ex = in.nextDouble();

    System.out.print("Enter quiz1: ");
    double q1 = in.nextDouble();

    System.out.print("Enter quiz2: ");
    double q2 = in.nextDouble();

    System.out.print("Are you done(yes/no): ");
    input = in.next();
}while(!"yes".equals(input));
Jérôme
  • 1,133
  • 2
  • 16
  • 21
user3645728
  • 45
  • 1
  • 9
  • Please look at [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Hovercraft Full Of Eels Mar 14 '17 at 00:10
  • I did look at that question, and i already understand what the problem is, but do not know how to fix it. I used the answer given in that question as well but it did not work for me as shown in my code. – user3645728 Mar 14 '17 at 00:13
  • The answers already tell you what to do. – Hovercraft Full Of Eels Mar 14 '17 at 00:15
  • And no where in your code above do I see you applying the answer. – Hovercraft Full Of Eels Mar 14 '17 at 00:15
  • Where do you call `in.nextLine()` after getting your doubles? Why aren't you changing `in.next()` to `in.nextLine()`? If you truly applied the answer in the previous question, you'd be doing this. – Hovercraft Full Of Eels Mar 14 '17 at 00:19
  • I took my id as a string and used parseInt to convert to int, but still doesnt work. I do not want to put an empty input.nextLine() after asking user for the name as that forces the user to have to press enter twice to go to the next question and that is just dumb coding. Is there something else i have missed? The problem is with the student name and the id and not with the double values. I tested my code without asking for the double values and still the same problem – user3645728 Mar 14 '17 at 00:20
  • 1
    No it doesn't. It means that you're doing it wrong. You make that call **after** any call that doesn't use nextLine, not before. RE-read the answer. – Hovercraft Full Of Eels Mar 14 '17 at 00:21
  • Okay ill try that – user3645728 Mar 14 '17 at 00:22

0 Answers0