Challenge - 5 Problems
String Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Java code reading a string?
Consider the following Java code that reads a string from the user and prints it back. What will be printed if the user inputs Hello World?
Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.next(); System.out.println(input); scanner.close(); } }
Attempts:
2 left
💡 Hint
Remember that scanner.next() reads input only until the first space.
✗ Incorrect
The method scanner.next() reads only the next token, which is the sequence of characters until the first whitespace. So it reads "Hello" and stops before the space.
❓ Predict Output
intermediate2:00remaining
What does this code print when reading a full line?
This Java code reads a full line of input from the user. What will it print if the user inputs Hello World?
Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); System.out.println(input); scanner.close(); } }
Attempts:
2 left
💡 Hint
scanner.nextLine() reads the entire line including spaces.
✗ Incorrect
The method scanner.nextLine() reads the whole line including spaces until the user presses Enter.
🔧 Debug
advanced2:30remaining
Why does this code skip input after reading an integer?
Look at this code snippet. The user inputs 5 and then Hello. Why does the program skip reading the string and print an empty line?
Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); String text = scanner.nextLine(); System.out.println("Number: " + number); System.out.println("Text: '" + text + "'"); scanner.close(); } }
Attempts:
2 left
💡 Hint
Think about what happens to the newline character after nextInt() reads the number.
✗ Incorrect
nextInt() reads only the integer but leaves the newline character in the input buffer. Then nextLine() reads that leftover newline as an empty string.
📝 Syntax
advanced1:30remaining
Which option causes a compilation error when reading input?
Which of the following Java code snippets will cause a compilation error?
Attempts:
2 left
💡 Hint
Check if the Scanner class has a method named readLine().
✗ Incorrect
Scanner class does not have a method called readLine(). The correct method to read a line is nextLine().
🚀 Application
expert2:30remaining
How many strings are printed by this code?
This Java program reads three lines of input and prints them. How many lines will it print if the user inputs:
Line1
Line2
Line3
Line1
Line2
Line3
Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); for (int i = 0; i < 3; i++) { String line = scanner.nextLine(); if (line.isEmpty()) { break; } System.out.println(line); } scanner.close(); } }
Attempts:
2 left
💡 Hint
The loop runs 3 times and prints each non-empty line.
✗ Incorrect
The loop reads and prints each line until 3 lines are read or an empty line is encountered. Since all inputs are non-empty, it prints all 3 lines.