The other Answers are correct and good. I would add two points:
- Using a
switch
statement for your logic may be clouding the intent rather than clarifying it. Using a series of if-then
in your particular case may make more sense.
- The
char
type in Java is obsolete, unable to represent even half of the characters defined in Unicode. While the use of char
in this particular code will work, I suggest you not make a habit of using char
. Instead, learn to use code point integer numbers instead.
You can easily determine the code point.
String input = "";
int codePoint = input.codePointAt( 0 );
128567
Call Character.toString
to get a String
object containing a single character, the character represented by that particular code point integer number.
Example code.
public class Token
{
public static Token get ( int codePoint )
{
if ( "-+=".contains( Character.toString( codePoint ) ) )
{
// Do operator stuff.
return new OperatorToken( codePoint );
} else if ( Character.isDigit( codePoint ) )
{
// Do digit stuff.
return new DigitToken( codePoint );
} else
{
// Do default stuff.
return new DefaultToken( codePoint );
}
}
}