Filtering only punctuation and currency

G

Glenn Meter

I've got a case where I want to build a MaskFormatter that only accepts
punctuation and currency symbols.

In looking at the APIs, it seems easy to test if a character is a
punctuation or currency symbol. But I'm not finding APIs to get the
list of punctuation and currency symbols programatically. I just need
something that will return the characters as a String. It seems like
this should be easy. What stupid thing am I missing?

Thanks,
Glenn
 
M

Michael Borgwardt

Glenn said:
I've got a case where I want to build a MaskFormatter that only accepts
punctuation and currency symbols.

In looking at the APIs, it seems easy to test if a character is a
punctuation or currency symbol. But I'm not finding APIs to get the list
of punctuation and currency symbols programatically. I just need
something that will return the characters as a String. It seems like
this should be easy. What stupid thing am I missing?

That's just not how the characters and character classes are
defined in Unicode or Java.

But it's simple enough to loop over all the 2^16 Java chars,
call the classification methods in java.lang.Character for
each of them and build the String for yourself.

Do that once and save the result in a file somewhere.
 
A

Ann

Michael Borgwardt said:
That's just not how the characters and character classes are
defined in Unicode or Java.

But it's simple enough to loop over all the 2^16 Java chars,
call the classification methods in java.lang.Character for
each of them and build the String for yourself.

Do that once and save the result in a file somewhere.

Maybe you can subclass isISOControl() in Character.java
---
/**
* Determines if the specified character is an ISO control
* character. A character is considered to be an ISO control
* character if its code is in the range <code>'\u0000'</code>
* through <code>'\u001F'</code> or in the range
* <code>'\u007F'</code> through <code>'\u009F'</code>.
*
* @param ch the character to be tested.
* @return <code>true</code> if the character is an ISO control
character;
* <code>false</code> otherwise.
*
* @see java.lang.Character#isSpaceChar(char)
* @see java.lang.Character#isWhitespace(char)
* @since 1.1
*/
public static boolean isISOControl(char ch) {
return (ch <= 0x009F) && ((ch <= 0x001F) || (ch >= 0x007F));
 
G

Glenn Meter

Thanks for the tips. In the end, it was easiest for me to just create
my own formatter:

DefaultFormatter nonAlphanumericNonWhitespaceFormatter =
new DefaultFormatter() {
public String valueToString(Object o) {
return o.toString();
}
public Object stringToValue(String s)
throws ParseException
{
int strLen = s.length();
for (int i = 0; i < strLen; i++) {
char curChar = s.charAt(i);
if (Character.isLetterOrDigit(curChar) ||
Character.isWhitespace(curChar)) {
throw new ParseException(s, i);
}
}
return s;
}
};
nonAlphanumericNonWhitespaceFormatter.setAllowsInvalid(false);

JFormattedTextField myFormattedField = new JFormattedTextField(
new DefaultFormatterFactory(
new DefaultFormatter(),
new DefaultFormatter(),

nonAlphanumericNonWhitespaceFormatter));


Glenn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top