{"id":"calculating-with-input","slug":"calculating-with-input","title":"Calculating with input","text":"Numbers read from the user work like any other numbers once they're converted. A common pattern: read, calculate, then print the result.\n\n```java\nScanner scanner = new Scanner(System.in);\nint price = Integer.valueOf(scanner.nextLine());\nint count = Integer.valueOf(scanner.nextLine());\nint total = price * count;\nSystem.out.println(\"Total: \" + total);\n```\n\n```input\n12\n4\n```\n\n```output\nTotal: 48\n```\n\nStoring the result in its own variable (`total` here) gives it a name, which makes the program easier to read than one long line. It also lets you use the result more than once.\n","task":"Read two whole numbers and print their sum. For the input `3` and `8` the program prints:\n\n```\nFirst number:\nSecond number:\nSum: 11\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        int sum = first + second;\n        System.out.println(\"Sum: \" + sum);\n    }\n}\n","hints":["Read both numbers with `Integer.valueOf(scanner.nextLine())`.","Calculate the sum into its own variable, then print `\"Sum: \" + sum`."],"tests":[{"name":"Test 1","stdin":"3\n8\n","expect":"First number:\nSecond number:\nSum: 11","hidden":false},{"name":"Test 2","stdin":"-5\n5\n","expect":"First number:\nSecond number:\nSum: 0","hidden":true},{"name":"Test 3","stdin":"1000\n234\n","expect":"First number:\nSecond number:\nSum: 1234","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read two whole numbers and print their difference (the first minus the second) and their product. For the input `9` and `4` the program prints:\n\n```\nDifference: 5\nProduct: 36\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        int a = Integer.valueOf(scanner.nextLine());\n        int b = Integer.valueOf(scanner.nextLine());\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        int a = Integer.valueOf(scanner.nextLine());\n        int b = Integer.valueOf(scanner.nextLine());\n        System.out.println(\"Difference: \" + (a - b));\n        System.out.println(\"Product: \" + (a * b));\n    }\n}\n","hints":["The difference is `a - b` and the product `a * b`.","Inside a printed line, put the calculation in parentheses: `\"Difference: \" + (a - b)`. The next step shows why."],"tests":[{"name":"Test 1","stdin":"9\n4\n","expect":"Difference: 5\nProduct: 36","hidden":false},{"name":"Test 2","stdin":"4\n9\n","expect":"Difference: -5\nProduct: 36","hidden":true},{"name":"Test 3","stdin":"-3\n7\n","expect":"Difference: -10\nProduct: -21","hidden":true}],"require":[],"forbid":[]},{"task":"Read three whole numbers and print their sum with a label. For the input `2`, `5` and `10` the program prints:\n\n```\nThe sum of the numbers is 17\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        int first = Integer.valueOf(scanner.nextLine());\n        int second = Integer.valueOf(scanner.nextLine());\n        int third = Integer.valueOf(scanner.nextLine());\n        int sum = first + second + third;\n        System.out.println(\"The sum of the numbers is \" + sum);\n    }\n}\n","hints":["Three reads, three variables.","`int sum = first + second + third;`"],"tests":[{"name":"Test 1","stdin":"2\n5\n10\n","expect":"The sum of the numbers is 17","hidden":false},{"name":"Test 2","stdin":"0\n0\n0\n","expect":"The sum of the numbers is 0","hidden":true},{"name":"Test 3","stdin":"-10\n4\n100\n","expect":"The sum of the numbers is 94","hidden":true}],"require":[],"forbid":[]}]}