{"id":"list-methods-contains","slug":"contains-and-index-of","title":"contains and indexOf","text":"The last module searched a list with a loop. Two list methods do that searching for you:\n\n- `list.contains(value)` is `true` when the value is in the list, and `false` when it isn't.\n- `list.indexOf(value)` gives the index of the value, or `-1` when it isn't in the list. If the value is there more than once, you get the first index.\n\n```java\nArrayList<String> colors = new ArrayList<>();\ncolors.add(\"red\");\ncolors.add(\"green\");\ncolors.add(\"blue\");\ncolors.add(\"green\");\n\nSystem.out.println(colors.contains(\"blue\"));\nSystem.out.println(colors.contains(\"pink\"));\nSystem.out.println(colors.indexOf(\"green\"));\nSystem.out.println(colors.indexOf(\"pink\"));\n```\n\n```output\ntrue\nfalse\n1\n-1\n```\n\nBoth compare with `equals`, the way you compare Strings yourself, so they also find text the user typed in. The comparison is exact: `\"Green\"` with a capital G is not in this list.\n\nThey work on number lists too: `numbers.contains(7)` and `numbers.indexOf(7)` look for the value 7.\n\n### Searching in an if\n\n`contains` gives a `boolean`, so the call can be the condition of an `if`. With `indexOf`, check for -1 before you use the index:\n\n```java\nArrayList<String> queue = new ArrayList<>();\nqueue.add(\"Ines\");\nqueue.add(\"Tomas\");\nqueue.add(\"Yuki\");\n\nif (queue.contains(\"Tomas\")) {\n    System.out.println(\"Tomas is in the queue\");\n}\n\nString name = \"Yuki\";\nint index = queue.indexOf(name);\nif (index == -1) {\n    System.out.println(name + \" is not in the queue\");\n} else {\n    System.out.println(name + \" has \" + index + \" people ahead\");\n}\n```\n\n```output\nTomas is in the queue\nYuki has 2 people ahead\n```\n\nYuki is at index 2, and the index is exactly the number of people before her.\n","task":"The program reads a seat and says whether it's taken. Fill in the blank with the right list method. For the input `B4` it prints:\n\n```\nSeat B4 is taken\n```\n","kind":"fill","seed":"import java.util.ArrayList;\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        ArrayList<String> taken = new ArrayList<>();\n        taken.add(\"A1\");\n        taken.add(\"A2\");\n        taken.add(\"B4\");\n\n        String seat = scanner.nextLine();\n        if (taken.[[contains]](seat)) {\n            System.out.println(\"Seat \" + seat + \" is taken\");\n        } else {\n            System.out.println(\"Seat \" + seat + \" is free\");\n        }\n    }\n}\n","solution":"import java.util.ArrayList;\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        ArrayList<String> taken = new ArrayList<>();\n        taken.add(\"A1\");\n        taken.add(\"A2\");\n        taken.add(\"B4\");\n\n        String seat = scanner.nextLine();\n        if (taken.contains(seat)) {\n            System.out.println(\"Seat \" + seat + \" is taken\");\n        } else {\n            System.out.println(\"Seat \" + seat + \" is free\");\n        }\n    }\n}\n","hints":["The method that answers `true` or `false` to \"is this value in the list?\"","`taken.contains(seat)`"],"tests":[{"name":"Test 1","stdin":"B4\n","expect":"Seat B4 is taken","hidden":false},{"name":"Test 2","stdin":"C1\n","expect":"Seat C1 is free","hidden":true},{"name":"Test 3","stdin":"A1\n","expect":"Seat A1 is taken","hidden":true}],"require":[],"forbid":[],"more":[{"task":"The list `finishers` holds the runners of a race in the order they finished. Read a name and print the place that runner finished in (the winner is place 1), or say they're not in the results. Use `indexOf`, without a loop. For the input `Kim` the program prints:\n\n```\nKim finished in place 3\n```\n\nand for the input `Otto` it prints `Otto is not in the results`.\n","kind":"code","seed":"import java.util.ArrayList;\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        ArrayList<String> finishers = new ArrayList<>();\n        finishers.add(\"Mona\");\n        finishers.add(\"Raj\");\n        finishers.add(\"Kim\");\n        finishers.add(\"Lea\");\n\n        String name = scanner.nextLine();\n\n    }\n}\n","solution":"import java.util.ArrayList;\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        ArrayList<String> finishers = new ArrayList<>();\n        finishers.add(\"Mona\");\n        finishers.add(\"Raj\");\n        finishers.add(\"Kim\");\n        finishers.add(\"Lea\");\n\n        String name = scanner.nextLine();\n        int index = finishers.indexOf(name);\n        if (index == -1) {\n            System.out.println(name + \" is not in the results\");\n        } else {\n            System.out.println(name + \" finished in place \" + (index + 1));\n        }\n    }\n}\n","hints":["`int index = finishers.indexOf(name);` and then an `if` on `index == -1`.","Indexes start at 0 and places at 1, so the place is `index + 1`. Put it in parentheses in the print line."],"tests":[{"name":"Test 1","stdin":"Kim\n","expect":"Kim finished in place 3","hidden":false},{"name":"Test 2","stdin":"Otto\n","expect":"Otto is not in the results","hidden":true},{"name":"Test 3","stdin":"Mona\n","expect":"Mona finished in place 1","hidden":true},{"name":"Test 4","stdin":"Lea\n","expect":"Lea finished in place 4","hidden":true}],"require":[{"pattern":"\\.indexOf\\s*\\(","message":"Find the runner with `indexOf`."}],"forbid":[{"pattern":"\\b(for|while)\\b","message":"Let `indexOf` do the searching, without a loop."}]},{"task":"Some visitors sign a guest book more than once. Read names until the line `done`, and keep each name only the first time it appears: add a name to the list only if the list doesn't contain it yet. Then print the names in the order they first signed, and how many different guests there were. For the input `Ada`, `Ben`, `Ada`, `Cleo`, `Ben` the program prints:\n\n```\nAda\nBen\nCleo\n3 different guests\n```\n","kind":"code","seed":"import java.util.ArrayList;\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        ArrayList<String> guests = new ArrayList<>();\n\n    }\n}\n","solution":"import java.util.ArrayList;\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        ArrayList<String> guests = new ArrayList<>();\n        while (true) {\n            String name = scanner.nextLine();\n            if (name.equals(\"done\")) {\n                break;\n            }\n            if (!guests.contains(name)) {\n                guests.add(name);\n            }\n        }\n\n        for (String guest : guests) {\n            System.out.println(guest);\n        }\n        System.out.println(guests.size() + \" different guests\");\n    }\n}\n","hints":["In the reading loop: `if (!guests.contains(name)) { guests.add(name); }`. The `!` turns \"contains\" into \"doesn't contain\".","The number of different guests is the size of the list."],"tests":[{"name":"Test 1","stdin":"Ada\nBen\nAda\nCleo\nBen\ndone\n","expect":"Ada\nBen\nCleo\n3 different guests","hidden":false},{"name":"Test 2","stdin":"Zed\nZed\nAmy\nZed\ndone\n","expect":"Zed\nAmy\n2 different guests","hidden":true},{"name":"Test 3","stdin":"Ola\nPer\nKai\nPer\nIvy\ndone\n","expect":"Ola\nPer\nKai\nIvy\n4 different guests","hidden":true}],"require":[{"pattern":"\\.contains\\s*\\(","message":"Check for a name already in the list with `contains`."}],"forbid":[]}]}