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
- Prompt for age and handle non-numeric input without crashing.
- Read multiple numbers on one line and compute their sum.