This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hello, when i use the Character.getType(_) method, it returns an int value, i'm wondering how to convert that int value, ie the 15 below, to a meaningfull literal like "CONTROL" or "isaCONTROL" character or something like that!
There's no good way to do it. As you've seen from the API page, Character has a bunch of static constants. How do you translate a number to a name? I guess you could do this:
But it seems wrong. Below is a way to do it using reflection. The code scans all field names in java.lang.Character, looking for the first byte that matches the getType() of the char. As a general approach that's 99% good enough in my experience, but your actual mileage may vary. It's still cheesy.
I can't think of a better approach, and the moral is: use enums where possible. If the char types were enums rather than bytes, toString() would automatically return the enum's name, which would be exactly what you want.
Here's the reflection code.
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Joined: Mar 10, 2004
Much Thanks Philip!
Have lots to study here, really appreicate the suggestions!