Bird
Raised Fist0
Javaprogramming~20 mins

Reading string input in Java - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the correct way to read a full line of text input from the user in Java?
easy
A. Use Scanner.nextLine() method
B. Use Scanner.nextInt() method
C. Use System.out.println() method
D. Use Scanner.read() method

Solution

  1. Step 1: Understand Scanner methods

    The Scanner class has different methods to read input. nextLine() reads the entire line as a string.
  2. Step 2: Identify the correct method for full line input

    nextInt() reads only integers, System.out.println() prints output, and Scanner.read() does not exist. So, nextLine() is correct.
  3. Final Answer:

    Use Scanner.nextLine() method -> Option A
  4. Quick Check:

    Read full line = nextLine() [OK]
Hint: Use nextLine() to read full text line input [OK]
Common Mistakes:
  • Using nextInt() to read strings
  • Confusing output methods with input
  • Forgetting to import Scanner
2. Which of the following is the correct syntax to create a Scanner object to read input from the keyboard?
easy
A. Scanner input = new Scanner(System.out);
B. Scanner input = new Scanner(System.file);
C. Scanner input = new Scanner(System.err);
D. Scanner input = new Scanner(System.in);

Solution

  1. Step 1: Understand Scanner constructor

    The Scanner constructor takes an InputStream to read from. System.in is the standard input stream (keyboard).
  2. Step 2: Identify correct stream for keyboard input

    System.out and System.err are output streams, and System.file does not exist. So, new Scanner(System.in) is correct.
  3. Final Answer:

    Scanner input = new Scanner(System.in); -> Option D
  4. Quick Check:

    Keyboard input = System.in [OK]
Hint: Use System.in to read keyboard input with Scanner [OK]
Common Mistakes:
  • Using System.out or System.err instead of System.in
  • Trying to read from non-existent System.file
  • Missing parentheses in Scanner constructor
3. What will be the output of the following Java code if the user inputs: Hello World
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();
  }
}
medium
A. Hello
B. Hello World
C. World
D. Compilation error

Solution

  1. Step 1: Understand nextLine() behavior

    The nextLine() method reads the entire line including spaces until the user presses Enter.
  2. Step 2: Analyze the input and output

    User inputs "Hello World" and presses Enter. The program reads the full line and prints it exactly.
  3. Final Answer:

    Hello World -> Option B
  4. Quick Check:

    nextLine() reads full line = Hello World [OK]
Hint: nextLine() reads entire line including spaces [OK]
Common Mistakes:
  • Assuming nextLine() reads only first word
  • Confusing next() with nextLine()
  • Forgetting to close Scanner
4. Identify the error in the following code snippet that reads a string input from the user:
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"?
medium
A. The program does not compile
B. The program throws a runtime exception
C. The program reads only "John" and ignores "Doe"
D. The program reads the full line "John Doe" correctly

Solution

  1. Step 1: Understand difference between next() and nextLine()

    next() reads input only until the first space, so it reads "John" only.
  2. Step 2: Analyze input and output behavior

    User inputs "John Doe" but next() stops at space, so "Doe" is ignored in this read.
  3. Final Answer:

    The program reads only "John" and ignores "Doe" -> Option C
  4. Quick Check:

    next() reads until space = partial input [OK]
Hint: Use nextLine() to read full input with spaces [OK]
Common Mistakes:
  • Using next() when full line is needed
  • Expecting next() to read multiple words
  • Not testing with multi-word input
5. You want to read multiple lines of user input until the user types "exit". Which code snippet correctly implements this using Scanner and nextLine()?
hard
A. Scanner sc = new Scanner(System.in); String line; do { line = sc.nextLine(); System.out.println(line); } while (!line.equals("exit")); sc.close();
B. Scanner sc = new Scanner(System.in); while (sc.nextLine() != "exit") { System.out.println(sc.nextLine()); } sc.close();
C. Scanner sc = new Scanner(System.in); String line = sc.next(); while (!line.equals("exit")) { System.out.println(line); line = sc.next(); } sc.close();
D. Scanner sc = new Scanner(System.in); while (true) { String line = sc.nextLine(); if (line == "exit") break; System.out.println(line); } sc.close();

Solution

  1. Step 1: Understand string comparison in Java

    Use line.equals("exit") to compare strings, not ==.
  2. 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).
  3. Final Answer:

    The do-while loop with equals("exit") -> Option A
  4. Quick Check:

    Use equals() and read once per loop [OK]
Hint: Use equals() for string compare and read input once per loop [OK]
Common Mistakes:
  • Using == instead of equals() for strings
  • Reading input multiple times per loop
  • Using next() instead of nextLine() for full lines
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard