Skip to content
Ameen Abozaid
BEG-011

Input and output

  • 6 Lessons
  • 71 min of reading
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Scanner
  • console input
  • formatting
  • parsing
  • input validation

Learn first

71 min of reading · 3 h of practice (estimate)

Learning outcomes

  1. Read text and numbers from the console with Scanner.
  2. Parse strings to numbers and handle input that is not a number (looping until valid, without exceptions yet).
  3. Format output with printf/String.format (width, decimals, alignment).
  4. Explain why user input must never be trusted.

Lessons

  1. 01Reading input with Scanner13 min read
  2. 02The nextInt / nextLine trap11 min read
  3. 03Parsing strings into numbers12 min read
  4. 04Formatting output11 min read
  5. 05Validating input: never trust the user12 min read
  6. 06Demo: building the receipt printer with input validation12 min read

Lab and exercises

Lab

BEG-011-LAB1 · Receipt printer

Outcomes: 1–4 · Time: ~3 h Read item names, quantities, and prices. Print an aligned receipt with line totals, subtotal, and tax. Reject invalid quantities (not a whole number, or less than 1) by asking again.

Example output:

text
Item        Qty    Price     Total
Pen           2     1.50      3.00
Notebook      1     4.25      4.25
Subtotal                      7.25
Tax (15%)                     1.09
Total                         8.34

Acceptance: columns aligned; invalid input re-prompted.

Exercises (assessment: five)

IDOutcomeTask
BEG-011-EX011Ask for a name and an age; print Ana is 30 years old.
BEG-011-EX021Ask for age and then full name, and make both work correctly (the trap).
BEG-011-EX032Read a decimal number as text and double it with Double.parseDouble.
BEG-011-EX042Keep asking for a whole number until the user enters one between 1 and 10.
BEG-011-EX053Print three products and prices in aligned columns with two decimals.
Show solutions
  • EX01 in.nextLine() for name, in.nextInt() for age, then printf("%s is %d years old.%n", name, age).
  • EX02 Add in.nextLine(); after nextInt() before reading the name.
  • EX03 double d = Double.parseDouble(in.nextLine()); System.out.println(d * 2);
  • EX04 Loop with hasNextInt(), discard bad input with in.next(), and check n >= 1 && n <= 10.
  • EX05 printf("%-12s %8.2f%n", name, price).

Outcome 4 is checked by the quiz (Q09, Q10) and by lab criterion "invalid input re-prompted".

Quiz

Outcomes: O1 Scanner · O2 parse and validate · O3 formatting · O4 never trust input.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Which reads a whole line?O1
  2. 2To use Scanner you must:O1
  3. 3nextLine() returns empty right after nextInt() because:O1
  4. 4Integer.parseInt("12") + 1 is:O2
  5. 5in.hasNextInt() tells you:O2
  6. 6%.2f prints:O3
  7. 7%-10s does what?O3
  8. 8%n in printf is:O3
  9. 9Why validate input?O4
  10. 10Best response to the quantity abc:O4

Practical part

Five exercises BEG-011-EX01 to EX05 plus the receipt printer. Pass: at least four exercises and a lab that re-prompts on invalid input.

Common mistakes

Skipped input after nextInt; crashing on non-numeric input; misaligned columns.

Assessment

5 exercises.

Career relevance

Input validation is the seed of JNR-014 (Bean Validation) and JNR-041 (injection).

Type to search levels, phases, modules and lessons.