0
        String command = "";
        Scanner sc = new Scanner(System.in);
        while (command!="exit")
        {
            System.out.println("Please enter command: ");
            command = sc.nextLine();
            System.out.println("You enter: "+command);
            if (command == "exit")
            {
                System.out.println("Exit program.");
            }
        }
        sc.close();

Haven't written java in a year and I forgot how scanner work. The code would never enter the if part when I enter exit. I tried next() and nextLine().

Harry
  • 29
  • 6

1 Answers1

0

Because operator == compares the address memory not the value for String variable. You need to use equals() method.

command.equals("exit")