{"id":"variables-mixing-types","slug":"mixing-input-types","title":"Mixing input types","text":"A program often reads several kinds of values. The recipe is the same every time: read one line with `scanner.nextLine()`, then convert it to the type you need.\n\n| To get | Write |\n|---|---|\n| a `String` | `scanner.nextLine()` |\n| an `int` | `Integer.valueOf(scanner.nextLine())` |\n| a `double` | `Double.valueOf(scanner.nextLine())` |\n| a `boolean` | `Boolean.valueOf(scanner.nextLine())` |\n\nEach read takes the next line of input, so the order of the reads has to match the order of the input.\n\n```java\nScanner scanner = new Scanner(System.in);\nString item = scanner.nextLine();\nint count = Integer.valueOf(scanner.nextLine());\ndouble weight = Double.valueOf(scanner.nextLine());\nSystem.out.println(count + \" x \" + item + \", \" + weight + \" kg each\");\n```\n\n```input\nmelon\n3\n1.25\n```\n\n```output\n3 x melon, 1.25 kg each\n```\n","task":"Read a name and then an age (an `int`), and print one sentence. For the input `Ada` and `36` the program prints:\n\n```\nName:\nAge:\nAda is 36 years old.\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(\"Name:\");\n        String name = scanner.nextLine();\n        System.out.println(\"Age:\");\n        int age = Integer.valueOf(scanner.nextLine());\n        System.out.println(name + \" is \" + age + \" years old.\");\n    }\n}\n","hints":["The name stays a String; the age is converted with `Integer.valueOf`.","`name + \" is \" + age + \" years old.\"` builds the sentence."],"tests":[{"name":"Test 1","stdin":"Ada\n36\n","expect":"Name:\nAge:\nAda is 36 years old.","hidden":false},{"name":"Test 2","stdin":"Linus\n12\n","expect":"Name:\nAge:\nLinus is 12 years old.","hidden":true},{"name":"Test 3","stdin":"Grace Hopper\n85\n","expect":"Name:\nAge:\nGrace Hopper is 85 years old.","hidden":true}],"require":[{"pattern":"Integer\\s*\\.\\s*(valueOf|parseInt)\\s*\\(","message":"Read the age as an `int` with `Integer.valueOf(...)`."}],"forbid":[],"more":[{"task":"Read four values in this order: a word, a whole number, a decimal number and true or false. Print each one with its label. For the input `pencil`, `12`, `0.8` and `true` the program prints:\n\n```\nText: pencil\nInteger: 12\nDouble: 0.8\nBoolean: true\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 text = scanner.nextLine();\n        int integer = Integer.valueOf(scanner.nextLine());\n        double decimal = Double.valueOf(scanner.nextLine());\n        boolean truth = Boolean.valueOf(scanner.nextLine());\n        System.out.println(\"Text: \" + text);\n        System.out.println(\"Integer: \" + integer);\n        System.out.println(\"Double: \" + decimal);\n        System.out.println(\"Boolean: \" + truth);\n    }\n}\n","hints":["Four reads, each converted to its own type, in the order of the input.","A hidden test gives `5` as the decimal number: stored in a `double`, it's printed as `5.0`."],"tests":[{"name":"Test 1","stdin":"pencil\n12\n0.8\ntrue\n","expect":"Text: pencil\nInteger: 12\nDouble: 0.8\nBoolean: true","hidden":false},{"name":"Test 2","stdin":"two words\n-3\n5\nFalse\n","expect":"Text: two words\nInteger: -3\nDouble: 5.0\nBoolean: false","hidden":true},{"name":"Test 3","stdin":"x\n0\n0.125\nnope\n","expect":"Text: x\nInteger: 0\nDouble: 0.125\nBoolean: false","hidden":true}],"require":[{"pattern":"Integer\\s*\\.\\s*(valueOf|parseInt)\\s*\\(","message":"Read the whole number with `Integer.valueOf(...)`."},{"pattern":"Double\\s*\\.\\s*(valueOf|parseDouble)\\s*\\(","message":"Read the decimal number with `Double.valueOf(...)`."},{"pattern":"Boolean\\s*\\.\\s*(valueOf|parseBoolean)\\s*\\(","message":"Read the last value with `Boolean.valueOf(...)`."}],"forbid":[]},{"task":"This time the input comes in a different order: a decimal number, true or false, a word, then a whole number. Print them in yet another order: the word, the whole number, the decimal number and the boolean. For the input `2.5`, `false`, `spoon` and `4` the program prints:\n\n```\nspoon\n4\n2.5\nfalse\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        double decimal = Double.valueOf(scanner.nextLine());\n        boolean truth = Boolean.valueOf(scanner.nextLine());\n        String word = scanner.nextLine();\n        int whole = Integer.valueOf(scanner.nextLine());\n        System.out.println(word);\n        System.out.println(whole);\n        System.out.println(decimal);\n        System.out.println(truth);\n    }\n}\n","hints":["Read in the order the input comes; print in the order the task asks.","Stored as a `double`, a decimal input like `3` is printed as `3.0`, so convert each value to the right type."],"tests":[{"name":"Test 1","stdin":"2.5\nfalse\nspoon\n4\n","expect":"spoon\n4\n2.5\nfalse","hidden":false},{"name":"Test 2","stdin":"3\nTRUE\nfork\n-1\n","expect":"fork\n-1\n3.0\ntrue","hidden":true},{"name":"Test 3","stdin":"-0.25\nyes\nbig plate\n100\n","expect":"big plate\n100\n-0.25\nfalse","hidden":true}],"require":[{"pattern":"Integer\\s*\\.\\s*(valueOf|parseInt)\\s*\\(","message":"Read the whole number with `Integer.valueOf(...)`."},{"pattern":"Double\\s*\\.\\s*(valueOf|parseDouble)\\s*\\(","message":"Read the decimal number with `Double.valueOf(...)`."},{"pattern":"Boolean\\s*\\.\\s*(valueOf|parseBoolean)\\s*\\(","message":"Read true or false with `Boolean.valueOf(...)`."}],"forbid":[]}]}