{"id":"variables-doubles-booleans","slug":"doubles-and-booleans","title":"Reading doubles and booleans","text":"Decimal numbers and true-or-false answers are read the same way: read a line, then convert it.\n\n- `Double.valueOf(text)` turns text like `2.5` or `-0.75` into a `double`.\n- `Boolean.valueOf(text)` gives `true` when the text is `true` in any mix of capitals (`TRUE`, `True`), and `false` for anything else.\n\n```java\nScanner scanner = new Scanner(System.in);\ndouble height = Double.valueOf(scanner.nextLine());\nboolean ready = Boolean.valueOf(scanner.nextLine());\nSystem.out.println(\"Height: \" + height);\nSystem.out.println(\"Ready: \" + ready);\n```\n\n```input\n2\nyes\n```\n\n```output\nHeight: 2.0\nReady: false\n```\n\nTwo things in that output are worth a second look:\n\n- The input was `2`, but the program prints `2.0`. A `double` is always printed with its decimal part.\n- The input was `yes`, which isn't `true`, so the boolean is `false`.\n","task":"Ask for a decimal number, read it as a `double` and print it. For the input `3.75` the program prints:\n\n```\nGive a decimal number:\nYou gave 3.75\n```\n\nA hidden test types a whole number: as a `double`, `4` is printed as `4.0`.\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(\"Give a decimal number:\");\n        double number = Double.valueOf(scanner.nextLine());\n        System.out.println(\"You gave \" + number);\n    }\n}\n","hints":["`Double.valueOf(scanner.nextLine())` reads a line and turns it into a `double`.","Keep the value in a `double` variable, so a whole number is printed with `.0`."],"tests":[{"name":"Test 1","stdin":"3.75\n","expect":"Give a decimal number:\nYou gave 3.75","hidden":false},{"name":"Test 2","stdin":"4\n","expect":"Give a decimal number:\nYou gave 4.0","hidden":true},{"name":"Test 3","stdin":"-0.5\n","expect":"Give a decimal number:\nYou gave -0.5","hidden":true}],"require":[{"pattern":"Double\\s*\\.\\s*(valueOf|parseDouble)\\s*\\(","message":"Turn the input into a `double` with `Double.valueOf(...)`."}],"forbid":[],"more":[{"task":"Ask whether it's raining, read the answer as a `boolean` with `Boolean.valueOf`, and print it. For the input `true` the program prints:\n\n```\nIs it raining?\nRaining: 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        System.out.println(\"Is it raining?\");\n        boolean raining = Boolean.valueOf(scanner.nextLine());\n        System.out.println(\"Raining: \" + raining);\n    }\n}\n","hints":["`boolean raining = Boolean.valueOf(scanner.nextLine());`","Hidden tests type `TrUe` and `maybe`. `Boolean.valueOf` already handles both: capitals don't matter, and anything that isn't `true` is `false`."],"tests":[{"name":"Test 1","stdin":"true\n","expect":"Is it raining?\nRaining: true","hidden":false},{"name":"Test 2","stdin":"TrUe\n","expect":"Is it raining?\nRaining: true","hidden":true},{"name":"Test 3","stdin":"maybe\n","expect":"Is it raining?\nRaining: false","hidden":true}],"require":[{"pattern":"Boolean\\s*\\.\\s*(valueOf|parseBoolean)\\s*\\(","message":"Turn the input into a `boolean` with `Boolean.valueOf(...)`."}],"forbid":[]},{"task":"Read a price (a `double`) and whether the customer is a member (a `boolean`), in that order, and print both with labels. For the input `12.5` and `false` the program prints:\n\n```\nPrice:\nMember?\nPrice: 12.5\nMember: false\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(\"Price:\");\n        double price = Double.valueOf(scanner.nextLine());\n        System.out.println(\"Member?\");\n        boolean member = Boolean.valueOf(scanner.nextLine());\n        System.out.println(\"Price: \" + price);\n        System.out.println(\"Member: \" + member);\n    }\n}\n","hints":["The price is read and converted with `Double.valueOf`, the answer with `Boolean.valueOf`.","Ask both questions first, then print the two labelled lines."],"tests":[{"name":"Test 1","stdin":"12.5\nfalse\n","expect":"Price:\nMember?\nPrice: 12.5\nMember: false","hidden":false},{"name":"Test 2","stdin":"3\nTRUE\n","expect":"Price:\nMember?\nPrice: 3.0\nMember: true","hidden":true},{"name":"Test 3","stdin":"0.99\nno\n","expect":"Price:\nMember?\nPrice: 0.99\nMember: false","hidden":true}],"require":[{"pattern":"Double\\s*\\.\\s*(valueOf|parseDouble)\\s*\\(","message":"Read the price with `Double.valueOf(...)`."},{"pattern":"Boolean\\s*\\.\\s*(valueOf|parseBoolean)\\s*\\(","message":"Read the answer with `Boolean.valueOf(...)`."}],"forbid":[]}]}