Coding/Learn/java/user input
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnScanner & Interactive Input
11 / 31 0%
Lesson 11 of 31
6 mins read

Scanner & Interactive Input

PreviousNext Lesson
In Plain English

The `Scanner` class from the `java.util` package is the standard tool for reading user input in console applications. It parses primitive types and strings using regular expressions from `System.in`.

The Mental Model

"Scanner is like an attentive stenographer listening to System.in. When you ask for an integer with .nextInt(), it listens until a number is typed and delivers it into your variable."

Deep Dive: How It Works

Import required: `import java.util.Scanner;` at the top of your file.

Instantiation: `Scanner scanner = new Scanner(System.in);`

Methods: `.nextLine()` (reads full line text), `.next()` (reads single word), `.nextInt()`, `.nextDouble()`, `.nextBoolean()`.

Closing: Call `scanner.close()` when finished to release system resources.

Syntax Blueprint

import java.util.Scanner;

Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int age = sc.nextInt();
sc.close();

Always import Scanner from java.util and instantiate with System.in.

PointCore Rules to Remember

Point star
Import Required: Must have import java.util.Scanner; at top of source file.
Point star
Type-Specific Readers: Use .nextInt() for numbers, .nextLine() for full string lines.
Point star
Standard Input: System.in represents the keyboard or standard input stream.

Common Beginner Traps & How to Fix Them

Mistake:Calling nextLine() immediately after nextInt()

Why it happens: nextInt() consumes the number but leaves the newline character in the buffer.

How to fix: Call an extra scanner.nextLine() to consume the leftover newline before reading text.

Pro Tip: In ZenCompiler, you can simulate input streams or construct Scanner with hardcoded test strings: new Scanner("Alice\n25").

Live Interactive Example

Hit Run Code to see it live
Reading and Processing Input
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Output Console
Click "Run Code" to view the rendered output.
How it works: Scanner reads the first line as String and the next token as an integer.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Read and double an input number

  • PointRead the integer from the scanner.
  • PointMultiply it by 2 and print "Double: " + (num * 2).
1
2
3
4
5
6
7
8
9
10
11
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates