0Pricing
Java Academy · Lesson

Input & Expressions – Part 2

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

Input & Expressions – Part 2 is a free Java Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Read Lines

Read lines with nextLine(), then trim() spaces.

String text = sc.nextLine().trim();

Parse Numbers

Parse numbers from strings:

  • int n = Integer.parseInt("42");
  • double x = Double.parseDouble("3.14");

Validate input in real apps to avoid crashes.

Concat vs Add

Concatenation vs addition

  • If either side is a String, + concatenates.
  • Use parentheses to control order: "Total: " + (a + b).
  • Cast or use double literals to control numeric division.

nextLine & Concat Demo

Try it: Enter two integers as text. Notice when + concatenates and when it adds.

public class Main {
  public static void main(String[] args) {
    // Example input values (instead of using Scanner)
    int a = 7;  
    int b = 3;

    // Print the sum of a and b
    System.out.println("a + b = " + (a + b));

    // Concatenating integer a with the string "b"
    System.out.println("a + \"b\" = " + (a + "b"));

    // String concatenation: "2" + 3 becomes "23"
    System.out.println("\"2\" + 3 = " + ("2" + 3));

    // Left-to-right evaluation: (2 + 3) = 5, then "5" + "4" = "54"
    System.out.println("2 + 3 + \"4\" = " + (2 + 3 + "4"));

    // Adding a and b first, then concatenating with the string "Total: "
    System.out.println("\"Total: \" + (a + b) = " + ("Total: " + (a + b)));
  }
}

Order & Concat

Quick check: What is the output of System.out.println(2 + 3 + "4"); ?

Recap & Next

Recap: You used nextLine(), trimmed input, parsed numbers, and controlled concatenation with parentheses.

Next: Move to conditions to make decisions based on input.

Frequently asked questions

Is the “Input & Expressions – Part 2” lesson free?

Yes — the full text of “Input & Expressions – Part 2” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Input & Expressions – Part 2”?

Use nextLine safely, trim whitespace, parse numbers from text, and control concatenation vs addition with parentheses. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Input & Expressions – Part 2” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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