Reading string input in Java - Time & Space Complexity
When we read a string input in Java, we want to know how the time it takes changes as the input gets bigger.
We ask: How does reading a longer string affect the time the program runs?
Analyze the time complexity of the following code snippet.
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
System.out.println("You entered: " + input);
scanner.close();
}
}
This code reads a full line of text from the user and then prints it back.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each character of the input line one by one internally.
- How many times: Once for each character in the input string.
As the input string gets longer, the time to read it grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character reads |
| 100 | About 100 character reads |
| 1000 | About 1000 character reads |
Pattern observation: The time grows directly with the number of characters; double the input, double the work.
Time Complexity: O(n)
This means the time to read the input grows linearly with the length of the string.
[X] Wrong: "Reading input always takes the same time no matter how long it is."
[OK] Correct: Actually, the program reads each character one by one, so longer input means more work and more time.
Understanding how input reading scales helps you write programs that handle big data smoothly and shows you think about efficiency.
"What if we read input character by character instead of line by line? How would the time complexity change?"
Practice
Solution
Step 1: Understand Scanner methods
TheScannerclass has different methods to read input.nextLine()reads the entire line as a string.Step 2: Identify the correct method for full line input
nextInt()reads only integers,System.out.println()prints output, andScanner.read()does not exist. So,nextLine()is correct.Final Answer:
Use Scanner.nextLine() method -> Option AQuick Check:
Read full line = nextLine() [OK]
- Using nextInt() to read strings
- Confusing output methods with input
- Forgetting to import Scanner
Solution
Step 1: Understand Scanner constructor
The Scanner constructor takes an InputStream to read from.System.inis the standard input stream (keyboard).Step 2: Identify correct stream for keyboard input
System.outandSystem.errare output streams, andSystem.filedoes not exist. So,new Scanner(System.in)is correct.Final Answer:
Scanner input = new Scanner(System.in); -> Option DQuick Check:
Keyboard input = System.in [OK]
- Using System.out or System.err instead of System.in
- Trying to read from non-existent System.file
- Missing parentheses in Scanner constructor
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
System.out.println(text);
sc.close();
}
}Solution
Step 1: Understand nextLine() behavior
ThenextLine()method reads the entire line including spaces until the user presses Enter.Step 2: Analyze the input and output
User inputs "Hello World" and presses Enter. The program reads the full line and prints it exactly.Final Answer:
Hello World -> Option BQuick Check:
nextLine() reads full line = Hello World [OK]
- Assuming nextLine() reads only first word
- Confusing next() with nextLine()
- Forgetting to close Scanner
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.next();
System.out.println(name);
sc.close();
}
}What is the problem if the user inputs "John Doe"?
Solution
Step 1: Understand difference between next() and nextLine()
next()reads input only until the first space, so it reads "John" only.Step 2: Analyze input and output behavior
User inputs "John Doe" butnext()stops at space, so "Doe" is ignored in this read.Final Answer:
The program reads only "John" and ignores "Doe" -> Option CQuick Check:
next() reads until space = partial input [OK]
- Using next() when full line is needed
- Expecting next() to read multiple words
- Not testing with multi-word input
Solution
Step 1: Understand string comparison in Java
Useline.equals("exit")to compare strings, not==.Step 2: Analyze loop structure and input reading
Scanner sc = new Scanner(System.in); String line; do { line = sc.nextLine(); System.out.println(line); } while (!line.equals("exit")); sc.close(); uses a do-while loop reading input once per iteration and compares correctly. Scanner sc = new Scanner(System.in); while (sc.nextLine() != "exit") { System.out.println(sc.nextLine()); } sc.close(); reads twice per loop and uses != with strings (wrong). Scanner sc = new Scanner(System.in); String line = sc.next(); while (!line.equals("exit")) { System.out.println(line); line = sc.next(); } sc.close(); uses next() which reads only one word. Scanner sc = new Scanner(System.in); while (true) { String line = sc.nextLine(); if (line == "exit") break; System.out.println(line); } sc.close(); uses == for string comparison (wrong).Final Answer:
The do-while loop with equals("exit") -> Option AQuick Check:
Use equals() and read once per loop [OK]
- Using == instead of equals() for strings
- Reading input multiple times per loop
- Using next() instead of nextLine() for full lines
Ready to prove it?
Pass the test and this pattern moves unknown
