{"id":"boolean-logic-strings","slug":"comparing-strings","title":"Comparing text with equals","text":"Text is compared with `equals`, not with `==`:\n\n```java\nScanner scanner = new Scanner(System.in);\nString first = scanner.nextLine();\nString second = scanner.nextLine();\nSystem.out.println(first.equals(second));\nSystem.out.println(first == second);\n```\n\n```input\ncat\ncat\n```\n\n```output\ntrue\nfalse\n```\n\nBoth lines are `cat`, yet `==` says false. For Strings, `==` doesn't check whether the texts are the same: it checks whether the two variables refer to the very same String object in memory. Every `nextLine()` that reads some text makes a new String, so two reads of the same word aren't `==`. `first.equals(second)` compares the characters, which is what you want. Module 22 explains objects and references properly.\n\nTo compare with a fixed word, write the text in quotes: `command.equals(\"quit\")`. Capitals count: `\"Quit\".equals(\"quit\")` is false. `equalsIgnoreCase` ignores them.\n","task":"Ask for a password and print `Welcome!` when it's exactly `swordfish` (capitals count, so `Swordfish` is wrong), otherwise `Wrong password`. For the input `swordfish` the program prints:\n\n```\nPassword:\nWelcome!\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        System.out.println(\"Password:\");\n        String password = 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        System.out.println(\"Password:\");\n        String password = scanner.nextLine();\n        if (password.equals(\"swordfish\")) {\n            System.out.println(\"Welcome!\");\n        } else {\n            System.out.println(\"Wrong password\");\n        }\n    }\n}\n","hints":["`password.equals(\"swordfish\")` is true when the text matches exactly.","With `==` the check would fail even for the right password."],"tests":[{"name":"Test 1","stdin":"swordfish\n","expect":"Password:\nWelcome!","hidden":false},{"name":"Test 2","stdin":"Swordfish\n","expect":"Password:\nWrong password","hidden":true},{"name":"Test 3","stdin":"12345\n","expect":"Password:\nWrong password","hidden":true}],"require":[],"forbid":[{"pattern":"(==|!=)\\s*\"\"|\"\"\\s*(==|!=)","message":"Compare text with `equals`, not `==`."}],"more":[{"task":"Read two words and print `Same` when they're exactly the same text (capitals count: `apple` and `Apple` are different), otherwise `Different`. For the input `apple` and `apple` the program prints:\n\n```\nSame\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        String first = scanner.nextLine();\n        String second = 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        String first = scanner.nextLine();\n        String second = scanner.nextLine();\n        if (first.equals(second)) {\n            System.out.println(\"Same\");\n        } else {\n            System.out.println(\"Different\");\n        }\n    }\n}\n","hints":["`first.equals(second)` compares the two texts.","`first == second` is false for two separate reads, even of the same word."],"tests":[{"name":"Test 1","stdin":"apple\napple\n","expect":"Same","hidden":false},{"name":"Test 2","stdin":"apple\nApple\n","expect":"Different","hidden":true},{"name":"Test 3","stdin":"same text\nsame text\n","expect":"Same","hidden":true}],"require":[{"pattern":"\\.equals\\s*\\(","message":"Compare the words with `equals`."}],"forbid":[]},{"task":"A tiny command menu. Read a command and answer:\n\n- `help` prints `Commands: help, hello, quit`\n- `hello` prints `Hi there!`\n- `quit` prints `Bye!`\n- anything else prints `Unknown command: ` and the command\n\nFor the input `hello` the program prints `Hi there!`.\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        String command = 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        String command = scanner.nextLine();\n        if (command.equals(\"help\")) {\n            System.out.println(\"Commands: help, hello, quit\");\n        } else if (command.equals(\"hello\")) {\n            System.out.println(\"Hi there!\");\n        } else if (command.equals(\"quit\")) {\n            System.out.println(\"Bye!\");\n        } else {\n            System.out.println(\"Unknown command: \" + command);\n        }\n    }\n}\n","hints":["An `else if` chain with one `equals` per command.","The final `else` prints `\"Unknown command: \" + command`."],"tests":[{"name":"Test 1","stdin":"hello\n","expect":"Hi there!","hidden":false},{"name":"Test 2","stdin":"help\n","expect":"Commands: help, hello, quit","hidden":true},{"name":"Test 3","stdin":"quit\n","expect":"Bye!","hidden":true},{"name":"Test 4","stdin":"dance\n","expect":"Unknown command: dance","hidden":true}],"require":[],"forbid":[{"pattern":"(==|!=)\\s*\"\"|\"\"\\s*(==|!=)","message":"Compare text with `equals`, not `==`."}]}]}