0

I want the code to be if entered "shoot" then it'll print "Nice, you killed the zombie" and if the user inputs "don't shoot" then it'll print "oh no, the zombie killed you"

this is what I've done so far but it won't print out anything.

    public static void main(String[] args) {
        System.out.println("ZOMBIE AHEAD!");
        Scanner kb = new Scanner(System.in);
        String action1 = "shoot";
        String action2 = "don't shoot";
        String str = kb.nextLine();
        if (str == action1) {
            System.out.println("Nice, you killed the zombie!");
        } else if (str == action2)  {
            System.out.println("Oh no, the zombie killed you!");
        }
    }
}
06ov
  • 17
  • 4
  • 4
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – OH GOD SPIDERS Sep 16 '20 at 16:44

1 Answers1

0

Use the following:

    public static void main(String[] args) {
        System.out.println("ZOMBIE AHEAD!");
        Scanner kb = new Scanner(System.in);
        String action1 = "shoot";
        String action2 = "don't shoot";
        String str = kb.nextLine();
        if (str.equals(action1)) {
            System.out.println("Nice, you killed the zombie!");
        } else if (str.equals(action2))  {
            System.out.println("Oh no, the zombie killed you!");
        }
    }
}

You have to use .equals() to compare Strings.

Spectric
  • 30,714
  • 6
  • 20
  • 43