Complete the code to read a line of text from the user using Scanner.
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(); } }
The nextLine() method reads a whole line of text from the user input.
Complete the code to read a single word from the user input.
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(); } }
The next() method reads the next token (word) from the input.
Fix the error in the code to correctly read a string input.
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(); } }
The method call must include parentheses () to invoke it. So nextLine() is correct.
Fill both blanks to read a word and then a full line from the user.
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(); } }
Use next() to read the first word, then nextLine() to consume the leftover newline before reading the full line.
Fill all three blanks to read two words and then print them in uppercase.
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(); } }
Use next() to read each word, then toUpperCase() to convert the first word to uppercase before printing.