0

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.

Angmar
  • 195
  • 6
  • 2
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – PiRocks Feb 16 '20 at 15:23
  • 1
    That is because if you compare two strings with == and they are equal, this will return 0. And so it never enters System.... because you do not ask for it. Use string1.equals(string2) for a real string comparison with a boolean as a return value. – funky Feb 16 '20 at 15:24
  • 1
    Perhaps you are confusing final with string internment? – PiRocks Feb 16 '20 at 15:24
  • That was what I thought, but my teacher asked me to declare them as final, so that "public static final String SHOW = "print"; if (command == SHOW { something should happen. He was the one that told me to use the == so that is why I am trying to use it. The problem is not that I don't know how to compare strings, the problem is that I don't know how to compare final strings using the variable. – Angmar Feb 16 '20 at 15:25
  • Also, I do not know what is a string internment. – Angmar Feb 16 '20 at 15:27
  • "He was the one that told me to use the == so that is why I am trying to use it." You cannot use `==` to compare string values in Java. There is an obscure/advanced trick that allows you to called string internment. I would just use `.equals` instead of `==` – PiRocks Feb 16 '20 at 15:29
  • I tried "if (command.equals(END))" but it does not work either. What am I doing wrong? – Angmar Feb 16 '20 at 15:31
  • 1
    debug your application. equals should work. Make sure that `command` really is what you think it is. – f1sh Feb 16 '20 at 15:32
  • Oh, I used the other variable (END instead of SHOW), so when I switched that it worked. Thanks! – Angmar Feb 16 '20 at 15:35

1 Answers1

2
String command = scanner.nextLine();
if (command.equals(SHOW) {
   System.out.println("Print this line.");
}

Use it like this. (To answer your comment)

funky
  • 313
  • 3
  • 8
  • 14