Java - User Input (Scanner)

User Input (Scanner)

import java.util.*;
Scanner sc = new Scanner(System.in);
System.out.print("Name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);

Use nextLine() to read lines; be careful when mixing nextInt() with nextLine() due to newline handling.

Try it

  1. Prompt for age and handle non-numeric input without crashing.
  2. Read multiple numbers on one line and compute their sum.