-1

I am making a parser for a language I call "Dint" in Java, and I added a statement for debugging: System.out.println("tok's val: "+tok); and it has the same value as the parser, but won't print "Found a print", why?

I have tried moving statements between lines, I have tried to look up the issue, and as a last resort I added that debugging statement, but I am confused as to why it won't work.

public class Dint {
    public static void main(String[] args) {
        System.out.println("Welcome Dint ISE v0.0.1! use \"help\" for help!\n");
        Scanner Input = new Scanner(System.in);
        String In = Input.nextLine();
        boolean Program = true;
        while(Program) {
            String tok = "";
            String string = "";
            int state = 0;
            String[] strings = new String[100];
            for(int i = 0; i < In.length();) {
                tok += In.charAt(i);
                if(tok == " ") {
                    tok = "";
                } else if(tok == "\"") {
                    tok = "";
                } else if(tok == "out") {
                    System.out.println("Found a print");
                    tok = "";
                }
                i++;
            }
            System.out.println("tok's value: "+tok);
            In = Input.nextLine();
        }
    }
}

I get

out
tok's val: out

When I expected to get:

out
Found a print
tok's val: out

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • once you fix the tests, how do you expect to get `tok's val: out` if you set `tok = "";` for every positive test? – jhamon Jul 19 '19 at 07:19

3 Answers3

0

Instead of tok == " " you should use tok.equals(" ")

Marce
  • 467
  • 1
  • 6
  • 10
0

Strings should be compared using .equals(String) and not using ==.

0

You need to use equals instead of ==.In comon case better to use expression like " ".equals(yourVariable) to avoid NPE.

Pavel
  • 1