0

I have googled and searched SO for a solution to this seemingly simple problem, but to no avail. Sorry for wasting space with this newbie question, but I cannot for the life of me figure out what is wrong.

I initialized a scanner object, then I set a string to be equal to the object.nextLine(). Then I checked whether the string matches the string "hello" in an if statement. When I run the program, I type in "hello" and press enter, but the if statement is not coming out as true. Here is the code:

private static void getCommand(){

    Scanner commandScanner = new Scanner(System.in);

    System.out.println("Enter your command");
    String commandString = command.nextLine();
    if(commandString == "hello"){
        System.out.println("it worked");
    }
}

This method worked perfectly for checking scanned ints and doubles, but it is not working for strings.

Another small issue with the code, but not my main focus, is that I am getting a warning in eclipse: "Resource leak - commandScanner is never closed."

0 Answers0