Lab 03 10 pts Week 1 Due Apr 7

Lab 03: User Input

Scanner methods, mixed input types, and the nextInt/nextLine trap

scanner nextInt nextLine buffer-trap
Clone from GitHub
3 checkpoints 5 autograder 2 timeliness
Prerequisites: lesson-1-8

After this lab, you will be able to:

  • Create a Scanner object and read input with nextLine, nextInt, and nextDouble
  • Identify and fix the nextInt/nextLine buffer trap
  • Read multiple values in sequence and format the output
  • Close the Scanner when input is complete

What You’re Building

You will write an interactive profile builder that prompts the user for a name, age, GPA, and hometown, then prints a formatted summary. The core challenge is correctly mixing nextInt (or nextDouble) with nextLine — the single most common Scanner bug in introductory Java.


Concepts and Misconceptions

Concept Common Mistake What the Test Catches
Scanner creation Writing Scanner(System.in) without new Compilation error
nextLine vs next Using next() for a full name (stops at first space) Name is truncated: "Ada" instead of "Ada Lovelace"
Buffer trap Calling nextLine() after nextInt() — gets empty string Hometown field is blank in output
Prompt format Missing or extra prompt text before each input Output/input sequence mismatch

Checkpoints

Checkpoint 1: Read Name and Age, Echo Back

What to do: Prompt for a name (full line) and age (integer). Print them back in the format: "Name: Ada Lovelace, Age: 25". Use nextLine() for the name and nextInt() for the age.

What the test checks: The output line matches the expected format exactly, including comma placement and spacing.

Debugging tip: If the name only captures the first word, you used next() instead of nextLine(). The next() method stops reading at whitespace; nextLine() reads until Enter.

Checkpoint 2: Fix the nextInt/nextLine Buffer Trap

What to do: After reading the age with nextInt(), read the hometown with nextLine(). You must consume the leftover newline character. Add a throwaway scanner.nextLine() call immediately after nextInt().

What the test checks: The hometown is not blank. If the buffer trap is unfixed, nextLine() returns "" and the test fails.

Debugging tip: The trap happens because nextInt() reads the number but leaves the \n in the buffer. The next nextLine() sees that \n and returns an empty string immediately. The fix is always the same: call scanner.nextLine() right after nextInt() to consume the leftover newline, then call nextLine() again for the actual input.

Checkpoint 3: Read Multiple Values, Format Output

What to do: Add GPA (nextDouble) to the profile. Print a complete summary with all four fields. Handle the buffer trap after nextDouble the same way as after nextInt.

What the test checks: All four fields appear in the summary with correct values. The GPA is printed to one decimal place.

Debugging tip: If GPA reads correctly but hometown is blank again, you forgot the throwaway nextLine() after nextDouble(). Every nextInt() or nextDouble() call needs a cleanup nextLine() before you can read a string.


How to Debug

  1. Test with the exact input the autograder uses. The test provides input via System.in redirection, not interactive typing. If your prompts print in the wrong order or you read the wrong type, the input sequence gets misaligned and everything after that point is wrong.

  2. Add temporary print statements. Print what each next* call returns: System.out.println("DEBUG name=[" + name + "]");. The brackets make it obvious if the string is empty or has extra whitespace. Remove debug prints before submitting.

  3. Count your nextLine() calls. After every nextInt() or nextDouble(), you need exactly one throwaway nextLine(). No more, no fewer. If you add an extra one, you will skip a prompt.


Scoring

Component Points Criteria
Checkpoints 3 1 pt each. Binary: the checkpoint test passes or it does not.
Autograder 5 Correctness across all test cases. Partial credit by proportion of tests passed.
Timeliness 2 Full credit if submitted by the due date. 0 if late.
Total 10