Challenge - 5 Problems
Scanner Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Reading an integer and a string using Scanner
What is the output of this Java program when the user inputs:
42
Hello
42
Hello
Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); sc.nextLine(); String text = sc.nextLine(); System.out.println("Number: " + num); System.out.println("Text: " + text); } }
Attempts:
2 left
💡 Hint
Remember that nextInt() does not consume the newline character.
✗ Incorrect
The nextInt() method reads the integer but leaves the newline character in the input buffer. The nextLine() then reads that leftover newline as an empty string. So the text variable is empty.
❓ Predict Output
intermediate2:00remaining
Reading multiple tokens with Scanner
What will be the output of this program if the user inputs:
John 25 true
John 25 true
Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name = sc.next(); int age = sc.nextInt(); boolean isStudent = sc.nextBoolean(); System.out.println(name + ", " + age + ", " + isStudent); } }
Attempts:
2 left
💡 Hint
next() reads one word, nextInt() reads an integer, nextBoolean() reads true/false.
✗ Incorrect
The Scanner reads tokens separated by whitespace. next() reads "John", nextInt() reads 25, nextBoolean() reads true. The output concatenates them with commas.
🔧 Debug
advanced2:00remaining
Why does this Scanner code skip input?
This code is supposed to read an integer and then a full line of text. But it skips reading the text input. Why?
Identify the problem and select the correct explanation.
Identify the problem and select the correct explanation.
Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter number: "); int n = sc.nextInt(); sc.nextLine(); System.out.print("Enter text: "); String line = sc.nextLine(); System.out.println("Number: " + n); System.out.println("Text: " + line); } }
Attempts:
2 left
💡 Hint
Think about what happens to the newline character after nextInt().
✗ Incorrect
nextInt() reads only the integer but leaves the newline character. nextLine() then reads that leftover newline as an empty string, causing it to skip the actual input.
📝 Syntax
advanced2:00remaining
Identify the syntax error in Scanner usage
Which option contains a syntax error when trying to read a double value using Scanner?
Attempts:
2 left
💡 Hint
Method names in Java are case-sensitive.
✗ Incorrect
Option B is invalid because nextDouble() does not take any arguments. Option C is invalid because nextDouble is a method and must be called with parentheses. Option D is invalid because method names are case-sensitive; nextdouble() is not defined.
🚀 Application
expert2:00remaining
How many tokens are read by Scanner?
Given this input line:
apple, banana; cherry orange
and this code:
Scanner sc = new Scanner(System.in); sc.useDelimiter(",|;| "); int count = 0; while(sc.hasNext()) { sc.next(); count++; } System.out.println(count);
What is the output?
apple, banana; cherry orange
and this code:
Scanner sc = new Scanner(System.in); sc.useDelimiter(",|;| "); int count = 0; while(sc.hasNext()) { sc.next(); count++; } System.out.println(count);
What is the output?
Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); sc.useDelimiter(",|;| "); int count = 0; while(sc.hasNext()) { sc.next(); count++; } System.out.println(count); } }
Attempts:
2 left
💡 Hint
Count the words separated by comma, semicolon, or space.
✗ Incorrect
The delimiters split the input on ",", ";", or space. Scanner skips consecutive delimiters (like ", " or "; "), so the tokens are "apple", "banana", "cherry", "orange" = 4 tokens.