Original Post
🔒 Locked
[java] testing a char
Advertisement
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:
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.
If you want to assign something to each char individually, use a Map instead.
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.
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
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.
IMHO that would be the most efficient way of handling your problem. Or by using the java.awt.event.KeyListener class.
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 remove if( charTyped == 8 || charTyped == 127 ) { //backspace //del myString = 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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement