• Advertisement
Sign in to follow this  

[java] testing a char

This topic is 4341 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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:

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement