I have created final strings for certain commands but when I try to check if the command given by the user matches, it does not work.
These are examples of my final strings:
public static final String END = "quit";
public static final String SHOW = "print";
But if I were to use it e.g. like this:
String command = scanner.nextLine();
if (command == SHOW) {
System.out.println("Print this line.");
}
It sees that they do not match even if they do. I tried printing the finalized variables:
System.out.println(END);
and it prints its value, but for some reason when I compare them to identical strings provided by the user, it does not recognize them.
I have tried to search similar examples and read about finalizing variables, but I think I am missing something important. I know that the problem is with the final variables and not elsewhere, because the program works if I compare these same strings with equal. E.g:
String command = scanner.nextLine();
if (command.equals("print") {
System.out.println("Print this line.");
}
I am assuming that there is something very simple that I am doing wrong, but I could not find any answer anywhere that I would understand. I am still a beginner and all the other examples regarding similar questions include some features that I don't understand, so they don't help me to see what I am doing wrong.
String command = scanner.nextLine();
if (command.equals(SHOW) {
System.out.println("Print this line.");
}
Use it like this. (To answer your comment)