0

I have a switch case that checking if char is "=" or "-" or "+" but I like to check also if char is a number from 0-9

    public static Token get(char ch) {
            switch (ch) {
                case '-' :
                case '+':
                case '=':
                    return new Token(ch);
                case  Character.isDigit(ch): // this dosn't work
                {
                }
            }
        }

what can I replace the Character.isDigit with?

user63898
  • 29,839
  • 85
  • 272
  • 514
  • 1
    Does this answer your question? [What is the best way to tell if a character is a letter or number in Java without using regexes?](https://stackoverflow.com/questions/4047808/what-is-the-best-way-to-tell-if-a-character-is-a-letter-or-number-in-java-withou) – Randy Casburn May 31 '21 at 05:10

3 Answers3

2

You could define explicit case statements for the ten digit characters:

public static Token get(char ch) {
    switch (ch) {
        case '-' :
        case '+':
        case '=':
            return new Token(ch);
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            // handle digit case
            break;
    }
}

Note here that we simply let all digit characters cases flow into a single point for handling.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

This would not work, since a switch case compares your char ch variable to the switch cases to get a match. Character.isDigit(ch): returns a boolean value, which cannot be directly compared to char ch.

I would suggest an alternate approach where you check for your condition in the default case.

public static Token get(char ch) {
    switch (ch) {
        case '-' :
        case '+':
        case '=':
            return new Token(ch);
        default:
        if(Character.isDigit(ch){
            //enter code here
        }
    }
}
Harsh
  • 26
  • 2
0

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 );
        }
    }
}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154