Search code examples
javaenumstokentokenize

Converting a double (the primitive type) to an enumerated type in Java


I'm trying to convert a String, which represents a function (eg: f(x) = 2.5x + 10) to an ArrayList of tokens. This is what I have for tokens:

public enum Tokens
{
    add,
    sub,
    mul,
    div,
    pow,
    exp,
    sin,
    cos,
    tan,
    sec,
    csc,
    cot,
    log,
    x,
    sta,
    end;
}

What I'd like to be able to do is basically cast a double to a "Tokens" type while preserving the value of the double so that constants can be represented in the same way for later processing, but I don't know how to accomplish this.


Solution

  • As you're discovering, your Tokens enum can't really be used for the actual tokens, because there's no way of making different versions of a Tokens.x (I'm guessing Tokens.x was meant to represent a number in a formula...) to hold different number values.

    What you need is a Token class--a "real" class--which can hold (at least) a token type and a value:

    public class Token {
        private TokenType type;
        private Object value;
        public Token(TokenType type) { 
            this.type = type; 
            this.value = null;
        }
        public Token(TokenType type, Object value) { 
            this.type = type;
            this.value = value;
        }
        // getters and setters for type and value omitted below
        // ...
    }
    

    TokenType would be an enum; the value attribute is an Object so it can hold any type of value object you might need for different token types.

    Now, there are a couple of ways to go when deciding how to use the type and value attributes to represent your tokens.

    One way would be rename your current Tokens enum to TokenType and use its values for your types. Most token types, such as TokenType.csc and TokenType.add, wouldn't need any value at all because the type tells you everything you need to know. But number tokens need to know their associated numeric value, so they would have a type of TokenType.x and the value would hold the numeric value as a Double, Integer, BigDecimal, or whatever.

    However, you'll probably find it more convenient to have token types that represent broader syntactic categories, such as "binary operator", "unary operator", "number", "end-of-formula":

    public enum TokenType {BINOP, UNOP, NUMBER, END};
    

    BINOP and UNOP tokens would use the value attribute to indicate specific operator they represent. Enums are perfect for this purpose:

    public enum BinOps {ADD, SUB, MUL, ... };
    public enum UnOps {SIN, COS, TAN, ... };  
    

    NUMBERs would carry their numeric value in the value attribute (again, as Double, Integer, BigDecimal, or whatever). END tokens wouldn't need any value.