I am making a calculator and part of this program takes in user String input and tokenizes it (using my own implementation of a Tokenizer class). So now I have a bunch of Token objects and I would like to test each one of them to see if they hold numbers or operators.
Is there a way to test to see if they hold operators (ie. +, -, *, /, =, (, ), etc.) without using
if (token.equals("+") || token.equals("-") || ... and so on, for each operator? These Token objects are all of type String.
If they’re all single-character strings you can do:
You could use an array to hold values that indicate the precedence of the corresponding token, too:
But since this all assumes that the operator is only one character, this is about as far as you can take this approach.