{"id":"variables-reading-integers","slug":"reading-integers","title":"Reading integers","text":"`scanner.nextLine()` always gives text, a String, even when the user types digits. To calculate with the number, the text has to be turned into an `int`. `Integer.valueOf(...)` does that:\n\n```java\nScanner scanner = new Scanner(System.in);\nSystem.out.println(\"How old are you?\");\nint age = Integer.valueOf(scanner.nextLine());\nSystem.out.println(\"Next year you'll be \" + (age + 1) + \".\");\n```\n\n```input\n29\n```\n\n```output\nHow old are you?\nNext year you'll be 30.\n```\n\nReading and converting fit on one line: the inner part, `scanner.nextLine()`, runs first and gives the text `\"29\"`, and `Integer.valueOf` turns it into the number `29`. (`age + 1` in parentheses is calculated before it's joined to the text; the next module is all about calculating.)\n\n`Integer.parseInt(...)` does the same job, and you'll see both in Java code. If the text isn't a whole number, for example `twenty` or `2.5`, the program stops with a `NumberFormatException`.\n","task":"Fill in the blank, so the program turns the line it reads into an `int`.\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        int number = Integer.[[valueOf‖parseInt]](scanner.nextLine());\n        System.out.println(\"You gave the number \" + number);\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        int number = Integer.valueOf(scanner.nextLine());\n        System.out.println(\"You gave the number \" + number);\n    }\n}\n","hints":["The method of `Integer` that turns text into a number is `valueOf`.","`Integer.valueOf(scanner.nextLine())` reads a line and converts it."],"tests":[{"name":"Test 1","stdin":"42\n","expect":"You gave the number 42","hidden":false},{"name":"Test 2","stdin":"-7\n","expect":"You gave the number -7","hidden":true},{"name":"Test 3","stdin":"0\n","expect":"You gave the number 0","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Ask for a lucky number, read it as an `int`, and print it in a sentence. For the input `7` the program prints:\n\n```\nWhat is your lucky number?\nYour lucky number is 7.\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(\"What is your lucky number?\");\n        int lucky = Integer.valueOf(scanner.nextLine());\n        System.out.println(\"Your lucky number is \" + lucky + \".\");\n    }\n}\n","hints":["Print the question, then read and convert: `int lucky = Integer.valueOf(scanner.nextLine());`","The full stop at the end goes in its own text: `... + lucky + \".\"`."],"tests":[{"name":"Test 1","stdin":"7\n","expect":"What is your lucky number?\nYour lucky number is 7.","hidden":false},{"name":"Test 2","stdin":"13\n","expect":"What is your lucky number?\nYour lucky number is 13.","hidden":true},{"name":"Test 3","stdin":"2026\n","expect":"What is your lucky number?\nYour lucky number is 2026.","hidden":true}],"require":[{"pattern":"Integer\\s*\\.\\s*(valueOf|parseInt)\\s*\\(","message":"Turn the input into an `int` with `Integer.valueOf(...)`."}],"forbid":[]},{"task":"Read two whole numbers, one per line, and print them on one line. No calculating yet. For the input `4` and `9` the program prints:\n\n```\nFirst number:\nSecond number:\nYou gave 4 and 9.\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(\"First number:\");\n        int first = Integer.valueOf(scanner.nextLine());\n        System.out.println(\"Second number:\");\n        int second = Integer.valueOf(scanner.nextLine());\n        System.out.println(\"You gave \" + first + \" and \" + second + \".\");\n    }\n}\n","hints":["Two questions and two reads, each read converted with `Integer.valueOf`.","`\"You gave \" + first + \" and \" + second + \".\"` joins the pieces."],"tests":[{"name":"Test 1","stdin":"4\n9\n","expect":"First number:\nSecond number:\nYou gave 4 and 9.","hidden":false},{"name":"Test 2","stdin":"100\n-5\n","expect":"First number:\nSecond number:\nYou gave 100 and -5.","hidden":true},{"name":"Test 3","stdin":"0\n0\n","expect":"First number:\nSecond number:\nYou gave 0 and 0.","hidden":true}],"require":[{"pattern":"Integer\\s*\\.\\s*(valueOf|parseInt)\\s*\\(","min":2,"message":"Turn both inputs into `int` values with `Integer.valueOf(...)`."}],"forbid":[]}]}