0

I am currently working on a project to parse strings into individual tokens. This is all and well, but I am stuck (obviously). I currently would like to test if a string matches a keyword (i.e. true, false, class, inherits, etc) and return that string as a token (please note I am not using the StringTokenizer that the Java API provides, IIRC).

Here is my code that pertains to the keywords:

if (isAlpha(ch)){
        System.out.println("Current character:" + ch);
        letters += ch;
        charIndex--;
        while(isDigit(nextChar()) || isAlpha(nextChar())){
            Character c = nextChar();
            System.out.println("C:" + c);
            letters += c;
            charIndex++;
            System.out.println("After another character:" + letters);
        }
        if(letters == "class"){
            token = new Token(token.CLASS, letters);
        }
        if(letters == "inherits"){
            token = new Token(token.INHERIT, letters);
        }
        if(letters == "if"){
            token = new Token(token.IF, letters);
        } //etc etc

I have isolated the problem in my testing to strings that end in an alphabetic character (i.e. "funny"; this token/string would be an identifier). It keeps looping on the "y" and prints "Current character: y." It works with strings like "funny6" though. I may be missing the obvious (having to do with whitespace, perhaps), but any advice would be appreciated.

Thank you!

user48326
  • 23
  • 2

0 Answers0