0
0
Javaprogramming~10 mins

Reading string input in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read a line of text from the user using Scanner.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.[1]();
        System.out.println("You entered: " + input);
        scanner.close();
    }
}
Drag options to blanks, or click blank then click option'
AnextBoolean
BnextInt
CnextDouble
DnextLine
Attempts:
3 left
💡 Hint
Common Mistakes
Using nextInt() instead of nextLine() causes input mismatch errors when reading text.
Forgetting to close the Scanner object.
2fill in blank
medium

Complete the code to read a single word from the user input.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String word = scanner.[1]();
        System.out.println("Word: " + word);
        scanner.close();
    }
}
Drag options to blanks, or click blank then click option'
Anext
BnextInt
CnextLine
DnextBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using nextLine() when only a single word is needed.
Using nextInt() which expects a number.
3fill in blank
hard

Fix the error in the code to correctly read a string input.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text = scanner.[1];
        System.out.println(text);
        scanner.close();
    }
}
Drag options to blanks, or click blank then click option'
AnextLine()
BnextLine
Cnext()
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after method names.
Using nextLine without parentheses causes a compile error.
4fill in blank
hard

Fill both blanks to read a word and then a full line from the user.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String firstWord = scanner.[1]();
        scanner.[2](); // consume leftover newline
        String fullLine = scanner.nextLine();
        System.out.println("First word: " + firstWord);
        System.out.println("Full line: " + fullLine);
        scanner.close();
    }
}
Drag options to blanks, or click blank then click option'
Anext
BnextLine
CnextInt
DnextBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Not consuming the leftover newline causes the nextLine() to read an empty string.
Using nextLine() for the first word instead of next().
5fill in blank
hard

Fill all three blanks to read two words and then print them in uppercase.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String word1 = scanner.[1]();
        String word2 = scanner.[2]();
        System.out.println(word1.[3]());
        System.out.println(word2.toUpperCase());
        scanner.close();
    }
}
Drag options to blanks, or click blank then click option'
Anext
BnextLine
CtoUpperCase
DnextInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using nextLine() to read words when next() is better for single words.
Forgetting parentheses after toUpperCase.