Examiner Warning
A. The Classic Buffer Skip Bug:
Methods like nextInt(), nextDouble(), and next() scan only the target tokens, leaving the trailing newline character (\n generated by the ENTER key) sitting unread in the keyboard stream.
System.out.print("Enter Roll No: ");
int roll = in.nextInt(); // User types 105 and presses ENTER (
)
System.out.print("Enter Full Name: ");
String name = in.nextLine(); // BUG! Immediately consumes the leftover '
' and skips!
B. The Fail-Safe Solution (Buffer Clearing):
Always insert an extra dummy in.nextLine(); immediately after reading numeric or single-word inputs before attempting to read a full line:
System.out.print("Enter Roll No: ");
int roll = in.nextInt();
in.nextLine(); // BUFFER FLUSH! Consumes and discards the orphan '
'
System.out.print("Enter Full Name: ");
String name = in.nextLine(); // Works flawlessly! Pauses for actual user input.