{"id":"reading-input-scanner","slug":"reading-a-line","title":"Reading a line with Scanner","text":"So far the programs printed the same thing every time. Most programs also take **input**: something the user types. In Java, a **Scanner** reads it. Using one takes three pieces:\n\n```java\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        System.out.println(\"Write a message:\");\n        String message = scanner.nextLine();\n        System.out.println(message);\n    }\n}\n```\n\n```input\nHello there\n```\n\n```output\nWrite a message:\nHello there\n```\n\n- `import java.util.Scanner;` at the very top tells the compiler where the Scanner comes from. Scanner is part of Java's standard library, in the package `java.util`, and a program has to import it before using it.\n- `Scanner scanner = new Scanner(System.in);` creates a Scanner called `scanner` that reads from `System.in`, the program's input. One Scanner is enough for the whole program.\n- `String message = scanner.nextLine();` waits until the user has typed a line and pressed Enter, then keeps that line in `message`. The next step explains what `String message` means.\n\n### Input on this site\n\nWhen you run a program in a terminal, you type the input while it runs, and your typing appears on the screen between the program's lines. The checks here give the program all of its input at the start instead, like a pre-typed answer sheet, so the expected output only contains what the program itself prints. That's why `Hello there` appears once in the output above, not twice.\n","task":"Fill in the two blanks: the class to import, and the Scanner method that reads a whole line. The program then prints back whatever line it reads.\n","kind":"fill","seed":"import java.util.[[Scanner‖*]];\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        String line = scanner.[[nextLine]]();\n        System.out.println(line);\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        String line = scanner.nextLine();\n        System.out.println(line);\n    }\n}\n","hints":["The import names the class the program uses, with the same capital letters.","The method that reads one whole line is `nextLine`, with a capital L."],"tests":[{"name":"Test 1","stdin":"Echo, echo\n","expect":"Echo, echo","hidden":false},{"name":"Test 2","stdin":"Anything at all\n","expect":"Anything at all","hidden":true},{"name":"Test 3","stdin":"  spaces at the start are kept\n","expect":"  spaces at the start are kept","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read one line, then print `You said:` followed by the line twice, each on its own line. For the input `Good morning` the program prints:\n\n```\nYou said:\nGood morning\nGood morning\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        // Read the line, then print the three lines.\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        // Read the line, then print the three lines.\n        String line = scanner.nextLine();\n        System.out.println(\"You said:\");\n        System.out.println(line);\n        System.out.println(line);\n    }\n}\n","hints":["`String line = scanner.nextLine();` reads the line once. You can print `line` as many times as you like.","`System.out.println(line);` prints the text that was read. Without quotes around `line`, Java prints its contents."],"tests":[{"name":"Test 1","stdin":"Good morning\n","expect":"You said:\nGood morning\nGood morning","hidden":false},{"name":"Test 2","stdin":"Java is fun\n","expect":"You said:\nJava is fun\nJava is fun","hidden":true},{"name":"Test 3","stdin":"42\n","expect":"You said:\n42\n42","hidden":true}],"require":[{"pattern":"\\.nextLine\\s*\\(\\s*\\)","max":1,"message":"Read the line only once, with one `nextLine()`, and print it twice."}],"forbid":[]},{"task":"Write the whole program: read one line and print it three times, between two lines of five equals signs. For the input `Java` it prints:\n\n```\n=====\nJava\nJava\nJava\n=====\n```\n","kind":"code","seed":"// Remember the import, the class and the main method.\n","solution":"// Remember the import, the class and the main method.\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        String text = scanner.nextLine();\n        System.out.println(\"=====\");\n        System.out.println(text);\n        System.out.println(text);\n        System.out.println(text);\n        System.out.println(\"=====\");\n    }\n}\n","hints":["The program needs `import java.util.Scanner;` at the top, then the class and main method.","Create the Scanner, read the line with `nextLine()`, then print five lines."],"tests":[{"name":"Test 1","stdin":"Java\n","expect":"=====\nJava\nJava\nJava\n=====","hidden":false},{"name":"Test 2","stdin":"Hello, world\n","expect":"=====\nHello, world\nHello, world\nHello, world\n=====","hidden":true},{"name":"Test 3","stdin":"x\n","expect":"=====\nx\nx\nx\n=====","hidden":true}],"require":[],"forbid":[]}]}