   [java] testing a char
  hello, i want to test keyboard input for validity.that means i have a long list of valid characters. whats the best method for checking the input char in terms of best performance?enumerations? collections? arrays?and what type should i use? char? byte? int? string?thanks!   
  If they are within a small range, use an array of char. For example, if the smallest valid char is 10 and the biggest valid char is 100, use an array of size 91. To see if a char is valid, use:public boolean validChar(char c){    return ((c >= 10) && (c <= 90) && validCharArray[c-10]);}Of course you have to fill the array with the proper values in the constructor.If the distribution of the chars is not very dense, you can use a Set of Character and put the valid ones there.// constructor{    validCharSet = new HashSet<Character>();    validCharSet.put('a');    validCharSet.put('e');    validCharSet.put('i');    validCharSet.put('o');    validCharSet.put('u');    validCharSet.put(123);}public boolean validChar(char c){    return validCharSet.contains(c);}If you want to assign something to each char individually, use a Map instead.   ;  You might find Character.isLetterOrDigit() handy.   ;  thanks,the distribution is very dense, so i will probably do the array version.the reason i am doing is, is mainly to prevent charactercodes coming from enter, shift, ctrl, alt, ... keys to be added to the string.i found that Character.isDefined() might do well, right?;  Just check out the Character javadoc. This class has some exellent methods for testing Characters. Good chance you can use one of those.http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html;  If your using the KeyListener to trap the events then it already separates a Standard latter character key from things like alt, ctrl, enter, arrow keys etc...The keyPressed is called when things like alt, ctrl, shift, or f1 are pressed. The keyTyped is called when any letter keys are pressed. I do believe that there is some overlap however so keyTyped gets called when tabs, backspace, or delete are pressed but additionally so does keyPressed.Also if you are polling for key input using a separate api or somehow just get the char and are trying to single out all letters, numbers, and punctuation characters. I would suggest a numerical test. if( (charTyped >= 32 && charTyped < 127) || charTyped == 9 ) {myString += charTyped;}else if( myString.length() > 0 ) { //do this only if there are characters in the string to removeif( charTyped == 8 || charTyped == 127 ) {//backspace//delmyString = myString.substring(0,myString()-1);}}IMHO that would be the most efficient way of handling your problem. Or by using the java.awt.event.KeyListener class.   