{"id":"loops-counting","slug":"counting","title":"Counting in a loop","text":"To count something in a loop, create a **counter** before the loop, starting at 0, and add one to it inside the loop each time the thing happens. After the loop, the counter holds the total.\n\n```java\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int count = 0;\n        while (true) {\n            String word = scanner.nextLine();\n            if (word.equals(\"end\")) {\n                break;\n            }\n            count++;\n        }\n        System.out.println(\"Words: \" + count);\n    }\n}\n```\n\n```input\napple\npear\nplum\nend\n```\n\n```output\nWords: 3\n```\n\n`count++` is Java's shorthand for `count = count + 1`. There's also `count--`, which takes one away.\n\n### Where the counter goes\n\nThe counter must be created **before** the loop. Created inside it, it would start again from 0 in every round, and it could never count past 1. The first challenge has exactly this mistake.\n","task":"This program should count the words before `end`, but it always prints `Words: 0`. Move the counter so it works. For the input `red`, `green`, `blue` and `end` the program prints:\n\n```\nWords: 3\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        while (true) {\n            int count = 0;\n            String word = scanner.nextLine();\n            if (word.equals(\"end\")) {\n                System.out.println(\"Words: \" + count);\n                break;\n            }\n            count++;\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 count = 0;\n        while (true) {\n            String word = scanner.nextLine();\n            if (word.equals(\"end\")) {\n                System.out.println(\"Words: \" + count);\n                break;\n            }\n            count++;\n        }\n    }\n}\n","hints":["`int count = 0;` runs every time the loop comes round, so the counter keeps going back to 0.","Move `int count = 0;` above the `while` line."],"tests":[{"name":"Test 1","stdin":"red\ngreen\nblue\nend\n","expect":"Words: 3","hidden":false},{"name":"Test 2","stdin":"end\n","expect":"Words: 0","hidden":true},{"name":"Test 3","stdin":"a\nb\nc\nd\ne\nend\n","expect":"Words: 5","hidden":true}],"require":[],"forbid":[],"more":[{"task":"The secret number is 42. Read guesses until one is right, then print how many guesses it took, the right one included. For the input `10`, `50` and `42` the program prints:\n\n```\nYou needed 3 guesses\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 guesses = 0;\n        while (true) {\n            int guess = Integer.valueOf(scanner.nextLine());\n            guesses++;\n            if (guess == 42) {\n                break;\n            }\n        }\n        System.out.println(\"You needed \" + guesses + \" guesses\");\n    }\n}\n","hints":["The right guess counts too, so add one to the counter before checking whether it's right.","Print the count after the loop."],"tests":[{"name":"Test 1","stdin":"10\n50\n42\n","expect":"You needed 3 guesses","hidden":false},{"name":"Test 2","stdin":"1\n2\n3\n4\n42\n","expect":"You needed 5 guesses","hidden":true},{"name":"Test 3","stdin":"41\n43\n42\n","expect":"You needed 3 guesses","hidden":true}],"require":[],"forbid":[]},{"task":"Read the lowest temperature of each day until `-99`. Print how many days had frost (below 0). For the input `3`, `-2`, `0`, `-7` and `-99` the program prints:\n\n```\nFrost days: 2\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 frostDays = 0;\n        while (true) {\n            int temperature = Integer.valueOf(scanner.nextLine());\n            if (temperature == -99) {\n                break;\n            }\n            if (temperature < 0) {\n                frostDays++;\n            }\n        }\n        System.out.println(\"Frost days: \" + frostDays);\n    }\n}\n","hints":["Count only when the temperature is below 0, so `frostDays++` goes inside an `if`.","0 itself isn't frost: the condition is `temperature < 0`."],"tests":[{"name":"Test 1","stdin":"3\n-2\n0\n-7\n-99\n","expect":"Frost days: 2","hidden":false},{"name":"Test 2","stdin":"-99\n","expect":"Frost days: 0","hidden":true},{"name":"Test 3","stdin":"-1\n-1\n-1\n-99\n","expect":"Frost days: 3","hidden":true}],"require":[],"forbid":[]}]}