0
0
Javaprogramming~20 mins

Reading string input in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
    }
}
AWorld
BHello World
CHello
DCompilation error
Attempts:
2 left
💡 Hint
Remember that scanner.next() reads input only until the first space.
Predict Output
intermediate
2: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();
    }
}
AWorld
BHello World
CHello
DRuntime error
Attempts:
2 left
💡 Hint
scanner.nextLine() reads the entire line including spaces.
🔧 Debug
advanced
2: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();
    }
}
AnextInt() leaves a newline in the buffer, so nextLine() reads it as empty
BnextLine() cannot read after nextInt() and causes an error
CThe code has a syntax error and does not compile
DnextInt() consumes the whole line including the string
Attempts:
2 left
💡 Hint
Think about what happens to the newline character after nextInt() reads the number.
📝 Syntax
advanced
1:30remaining
Which option causes a compilation error when reading input?
Which of the following Java code snippets will cause a compilation error?
A
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
scanner.close();
B
;)(esolc.rennacs
;)(eniLtxen.rennacs = tupni gnirtS
;)ni.metsyS(rennacS wen = rennacs rennacS
C
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
scanner.close();
D
Scanner scanner = new Scanner(System.in);
String input = scanner.readLine();
scanner.close();
Attempts:
2 left
💡 Hint
Check if the Scanner class has a method named readLine().
🚀 Application
expert
2: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
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();
    }
}
A3
B2
C0
D1
Attempts:
2 left
💡 Hint
The loop runs 3 times and prints each non-empty line.