0

I have this code:

while (length < content.length()) {
        tok += chars.get(length);
        tok = tok.toLowerCase();
        if (chars.get(length) == ' ') tok = "";
        else if (tok == "print") System.out.println("FOUND PRINT!");
        System.out.println(tok);
        length++;
    }

What I'm trying to do is to create a programming language with this. But when I try to detect if (tok == "print") it doesn't detect it. I used an ArrayList to store all chars from a string to that array, and then use a while loop to loop the string's length which you can see in the code. Then I add every char from the array list to the string and when I try detecting if the string is something it won't work.

Output of this code:

[P, R, I, N, T,  , ", H, E, L, L, O, "]
p 
pr 
pri 
prin
print - I'm trying to detect this

"
"h
"he
"hel
"hell
"hello
"hello"
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Владимир
  • 135
  • 1
  • 10
  • Use tok.equals("print") – Unknown Feb 14 '18 at 07:51
  • 1
    Wow thx im rly stupid lol XD – Владимир Feb 14 '18 at 07:52
  • 1
    Reason being, == "print" always compares the reference of the objects you are comparing. As per JVM, any two objects on the heap will always be different as their references are not same. This is the reason, .equals() of String class is overridden to compare the elements. – Jayanth Feb 14 '18 at 07:55

0 Answers0