Error in code Processing

The error is related to char but I can’t figure out what the problem is
public final class Lexer{
private static final String OPERATORS_CHARS = “±*/”;
private static final TokenType OPERATOR_TOKENS ={
TokenType.PLUS, TokenType.MINUS,
TokenType.STAR, TokenType.SLASH,
};
private final String input;
private final int length;
private final List tokens;
private int pos;

public Lexer(String input){
this.input = input;
length = input.length();

tokens = new ArrayList<>();
}

public List tokenize(){
while(pos < length){
final char current = peek();
if(Character.isDigit(current)) tokenizeNumber();
else if(OPERATOR_CHARS.indexOf(current) != -1) {
tokenizeOperator();
} else{
next();
}
}
return tokens;
}

private void tokenizeNumber(){
final StringBuilder buffer = new StringBuilder();
char current = peek();
while(Character.isDigit(current)){
buffer.append(current);
current = next();
}
addToken(TokenType.NUMBER, buffer.toString());
}

private void tokenizeOperator(){
final int position = OPERATOR.CHARS.indexOf(peek(0));
addToken(OPERATOR_TOKENS[position]);
next();
}

private char next(){
pos++;
return peek(0);
}

private char peek(int relativePosition){
final int position = pos + relativePosition;
if(position >= length) return ‘/0’;
return input.charAt(position);
}

private void addToken(TokenType type){
addToken(type, “”);
}

private void addToken(TokenType type, String text){
tokens.add(new Token(type, text));
}

}

1 Like

return '0'; // or null

Full class


class Lexer {
  private static final String OPERATORS_CHARS = "±*/";
  private static final TokenType OPERATOR_TOKENS ={
    TokenType.PLUS, TokenType.MINUS,
    TokenType.STAR, TokenType.SLASH,
  };
  private final String input;
  private final int length;
  private final List tokens;
  private int pos;

  public Lexer(String input) {
    this.input = input;
    length = input.length();

    tokens = new ArrayList<>();
  }

  public List tokenize() {
    while (pos < length) {
      final char current = peek();
      if (Character.isDigit(current)) tokenizeNumber();
      else if (OPERATOR_CHARS.indexOf(current) != -1) {
        tokenizeOperator();
      } else {
        next();
      }
    }
    return tokens;
  }

  private void tokenizeNumber() {
    final StringBuilder buffer = new StringBuilder();
    char current = peek();
    while (Character.isDigit(current)) {
      buffer.append(current);
      current = next();
    }
    addToken(TokenType.NUMBER, buffer.toString());
  }

  private void tokenizeOperator() {
    final int position = OPERATOR.CHARS.indexOf(peek(0));
    addToken(OPERATOR_TOKENS[position]);
    next();
  }

  private char next() {
    pos++;
    return peek(0);
  }

  private char peek(int relativePosition) {
    final int position = pos + relativePosition;
    if (position >= length)
      return '0'; // or null
    return input.charAt(position);
  }

  private void addToken(TokenType type) {
    addToken(type, "");
  }

  private void addToken(TokenType type, String text) {
    tokens.add(new Token(type, text));
  }
}

1 Like