Java•Stdin Parsing
Java Stdin Input: Scanner nextInt(), nextLine(), and BufferedReader
In Compilra OpenJDK 17 LTS, standard input is passed upfront before execution begins. Mixing Scanner.nextInt() with Scanner.nextLine() is a classic pitfall—learn how to clear the leftover newline or parse split lines cleanly.
Syntax
int n = sc.nextInt(); sc.nextLine(); String line = sc.nextLine();
Parameters & Values
Key components and acceptable values for this structure:
| Parameter | Type | Description |
|---|---|---|
| Scanner.nextInt() | int | Parses the next integer token without consuming the trailing line break. |
| sc.nextLine() | String | Consumes the rest of the current line and returns characters up to the newline. |
| BufferedReader | java.io.BufferedReader | Alternative fast line reader preferred for very large competitive programming inputs. |
Ready-to-Run Code Example
Executable in CompilraRun this complete, working example directly without installing Java locally:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if (!sc.hasNextInt()) return;
int count = sc.nextInt();
sc.nextLine();
for (int i = 1; i <= count; i++) {
String fullName = sc.nextLine().trim();
String[] tokens = sc.nextLine().trim().split("\\s+");
double sum = 0.0;
for (String token : tokens) {
sum += Double.parseDouble(token);
}
double avg = sum / tokens.length;
System.out.printf("#%d %s: avg = %.1f%n", i, fullName, avg);
}
sc.close();
}
}Pre-Supplied Stdin (Input):
2 Grace Hopper 92 98 Linus Torvalds 88 90 92
Expected Output:
#1 Grace Hopper: avg = 95.0 #2 Linus Torvalds: avg = 90.0
How It Works: Step-by-Step
- Lines 5-8: Checks sc.hasNextInt(), reads count, and calls sc.nextLine() to consume the remaining newline.
- Lines 10-12: Reads each full name line and splits the score line on whitespace regex \s+.
- Lines 13-18: Sums parsed doubles and prints each average with System.out.printf.
Key Concepts
- Compilra compiles public class Main under OpenJDK 17 LTS and feeds the pre-supplied Stdin box to System.in.
- Always check sc.hasNextInt() or sc.hasNextLine() when reading optional stdin so empty input exits cleanly.
- For tight time limits with thousands of lines, wrap InputStreamReader(System.in) in BufferedReader and use String.split or StringTokenizer.
Common Mistake: Skipping a line after sc.nextInt()
nextInt() stops before the newline character; without a dummy sc.nextLine(), the next sc.nextLine() returns an empty string.
❌ Incorrect
int n = sc.nextInt(); String name = sc.nextLine(); // Captures "" instead of the name!
✅ Correct
int n = sc.nextInt(); sc.nextLine(); // Clear trailing newline String name = sc.nextLine();
Related Java Tutorials & Compiler
Java Input and Output: Scanner, System.out, and printf
Read inputs using java.util.Scanner and format output streams with System.out.printf.
Java String Methods, StringBuilder, and Immutability
Master String methods (split, substring, replace) and mutable StringBuilder for performance.
Java Try-With-Resources and File I/O (AutoCloseable)
Ensure automatic resource closing using Java 7+ try-with-resources statement.
Java Online Compiler
Open in Compilra IDE
Run and test Java on Compilra
Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.