0Pricing
Java Academy · Lesson

Input & Expressions – Part 2

Use nextLine safely, trim whitespace, parse numbers from text, and control concatenation vs addition with parentheses.

Lesson Goals

Goals: Use nextLine() safely, trim input, parse numbers from text, and understand when + concatenates vs adds.

Newline Trap

Newline trap: After nextInt() or nextDouble(), a leftover newline may cause the next nextLine() to return empty.

Fix: call an extra nextLine() to consume it, or read lines and parse numbers.

All lessons in this course

  1. Variables & Data Types
  2. Input & Expressions – Part 1
  3. Input & Expressions – Part 2
← Back to Java Academy