{"id":"reading-input-several-inputs","slug":"several-inputs","title":"Several inputs in order","text":"Each call to `nextLine()` reads the next line of input, so a program can read as many lines as it needs, one after another. They're read in order: the first `nextLine()` gets the first line, the second gets the second, and so on. The program waits at each `nextLine()` until a line is there.\n\n```java\nScanner scanner = new Scanner(System.in);\nSystem.out.println(\"First word?\");\nString first = scanner.nextLine();\nSystem.out.println(\"Second word?\");\nString second = scanner.nextLine();\nSystem.out.println(second + \" \" + first);\n```\n\n```input\nworld\nhello\n```\n\n```output\nFirst word?\nSecond word?\nhello world\n```\n\nThe variables keep their values, so the program can print them in any order and as often as it likes. Only the reading has to follow the order of the input.\n\nIf a program asks for more lines than the input has, it stops with an error: `NoSuchElementException: No line found`. When that happens on this site, the result explains it. It usually means the program reads one line too many.\n","task":"Ask two questions and print the answers. For the input `blue` and `whale` the program prints:\n\n```\nFavorite color?\nFavorite animal?\nColor: blue\nAnimal: whale\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\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        System.out.println(\"Favorite color?\");\n        String color = scanner.nextLine();\n        System.out.println(\"Favorite animal?\");\n        String animal = scanner.nextLine();\n        System.out.println(\"Color: \" + color);\n        System.out.println(\"Animal: \" + animal);\n    }\n}\n","hints":["Ask, read, ask, read: print a question, then read its answer with `nextLine()`.","Keep each answer in its own variable, for example `color` and `animal`, and print them at the end."],"tests":[{"name":"Test 1","stdin":"blue\nwhale\n","expect":"Favorite color?\nFavorite animal?\nColor: blue\nAnimal: whale","hidden":false},{"name":"Test 2","stdin":"green\nfrog\n","expect":"Favorite color?\nFavorite animal?\nColor: green\nAnimal: frog","hidden":true},{"name":"Test 3","stdin":"dark red\nred panda\n","expect":"Favorite color?\nFavorite animal?\nColor: dark red\nAnimal: red panda","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read three lines and print them back in reverse order. For the input `one`, `two` and `three` the program prints:\n\n```\nthree\ntwo\none\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\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 first = scanner.nextLine();\n        String second = scanner.nextLine();\n        String third = scanner.nextLine();\n        System.out.println(third);\n        System.out.println(second);\n        System.out.println(first);\n    }\n}\n","hints":["Read all three lines first, each into its own variable.","Then print the variables starting from the last one read."],"tests":[{"name":"Test 1","stdin":"one\ntwo\nthree\n","expect":"three\ntwo\none","hidden":false},{"name":"Test 2","stdin":"red\ngreen\nblue\n","expect":"blue\ngreen\nred","hidden":true},{"name":"Test 3","stdin":"first line\nmiddle line\nlast line\n","expect":"last line\nmiddle line\nfirst line","hidden":true}],"require":[],"forbid":[]},{"task":"Write a tiny story generator. Ask for a hero's name and a place, then print a three-line story that uses each of them more than once. For the input `Ada` and `Helsinki` the program prints:\n\n```\nHero's name?\nA place?\nOnce upon a time, Ada set off for Helsinki.\nAda walked for days and days.\nWhen Ada finally reached Helsinki, the whole town cheered.\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\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        System.out.println(\"Hero's name?\");\n        String hero = scanner.nextLine();\n        System.out.println(\"A place?\");\n        String place = scanner.nextLine();\n        System.out.println(\"Once upon a time, \" + hero + \" set off for \" + place + \".\");\n        System.out.println(hero + \" walked for days and days.\");\n        System.out.println(\"When \" + hero + \" finally reached \" + place + \", the whole town cheered.\");\n    }\n}\n","hints":["Read the two answers into variables first. Each variable can then be used as many times as you need.","Build each story line with `+`, for example `\"Once upon a time, \" + hero + \" set off for \" + place + \".\"`.","The apostrophe in `Hero's` is fine inside double quotes."],"tests":[{"name":"Test 1","stdin":"Ada\nHelsinki\n","expect":"Hero's name?\nA place?\nOnce upon a time, Ada set off for Helsinki.\nAda walked for days and days.\nWhen Ada finally reached Helsinki, the whole town cheered.","hidden":false},{"name":"Test 2","stdin":"Captain Byte\nthe Moon\n","expect":"Hero's name?\nA place?\nOnce upon a time, Captain Byte set off for the Moon.\nCaptain Byte walked for days and days.\nWhen Captain Byte finally reached the Moon, the whole town cheered.","hidden":true},{"name":"Test 3","stdin":"Mo\nRome\n","expect":"Hero's name?\nA place?\nOnce upon a time, Mo set off for Rome.\nMo walked for days and days.\nWhen Mo finally reached Rome, the whole town cheered.","hidden":true}],"require":[],"forbid":[]}]}