Nishant
Nishant

Reputation: 23

Is there eny more efficient (and less tedious) way to do this?

For homework, I have to create my own method based on Character.isJavaIdentifierStart. It should consider only the characters ‘a’ through ‘z’ (lower and upper case), ‘$’, and ‘_’ to be valid. It returns a boolean value. Am I missing a simpler way to do this?

public static boolean myIsJavaIdentifierStart(char ch) {
    if (ch == '$' || ch == '_') {
        return true;
    }
    else {
        switch (ch) {
            case 'a':return true;
            case 'b':return true;
            case 'c':return true;
            case 'd':return true;
            case 'e':return true;
            case 'f':return true;
            case 'g':return true;
            case 'h':return true;
            case 'i':return true;
            case 'j':return true;
            case 'k':return true;
            case 'l':return true;
            case 'm':return true;
            case 'n':return true;
            case 'o':return true;
            case 'p':return true;
            case 'q':return true;
            case 'r':return true;
            case 's':return true;
            case 't':return true;
            case 'u':return true;
            case 'v':return true;
            case 'w':return true;
            case 'x':return true;
            case 'y':return true;
            case 'z':return true;
            case 'A':return true;
            case 'B':return true;
            case 'C':return true;
            case 'D':return true;
            case 'E':return true;
            case 'F':return true;
            case 'G':return true;
            case 'H':return true;
            case 'I':return true;
            case 'J':return true;
            case 'K':return true;
            case 'L':return true;
            case 'M':return true;
            case 'N':return true;
            case 'O':return true;
            case 'P':return true;
            case 'Q':return true;
            case 'R':return true;
            case 'S':return true;
            case 'T':return true;
            case 'U':return true;
            case 'V':return true;
            case 'W':return true;
            case 'X':return true;
            case 'Y':return true;
            case 'Z':return true;
            default: return false;
        }
    }
}

Upvotes: 2

Views: 119

Answers (3)

Jobs
Jobs

Reputation: 3377

There are certainly more efficient ways of doing this. 1. characters 'a' to 'z' in standards such as Unicode and ASCII are consecutive integers. Therefore, instead of checking each individually, you can do if(ch >0 'a' && ch <= 'z'). Additionally, you can try using : return (Character.isLetter(ch));

See the following references: - a list of all java character functions: http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html

Upvotes: 2

Troncoso
Troncoso

Reputation: 2463

if ((ch >= 'a' && ch <= 'z') || (ch >='A' && ch <= 'Z'))
    return true;

Or if you want to convert it to a String/Character, you can use toLower:

Character lower = Character.toLowerCase(ch);
if (ch >= 'a' && ch <= 'z')
    return true;

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

There are many other ways of doing this, but perhaps one of the simplest ones would be to expand upon something that you already have at the top of your method:

return ch == '$'
    || ch == '_'
    || (ch >= 'a' && ch <= 'z')
    || (ch >= 'A' && ch <= 'Z');

You need to note a couple of things to see why this works:

  • You are returning a boolean, so any Boolean expression can be used directly, without an if statement
  • Code points of letters A..Z and a..z in UNICODE are consecutive, enabling you to check for membership in [A..Z] or [a..z] intervals with a pair of >= and <= checks.

Upvotes: 11

Related Questions