Reputation: 97
Let's say we desire to have a non-ASCII character, for example, U+2082 (subscript 2).
Normally, we can display this in a swing component, such as JFrame, as Character.toString('\u2082')
.
Now, my issue is that I can't determine the the exact Unicode code, since the exact code is determined by the String supplied in the parameter. The parameter will always be a polyatomic ion - e.g. PO3. What my goal is to find the "3", turn that into a subscript 3 (U+2083), but also have the algorithm/method be abstracted enough that it will apply for any polyatomic ion (not just PO3, but also PO4 as well), and have it display correctly on a JFrame. I supply my method below.
private static String processName(String original)
{
char[] or = original.toCharArray();
int returned = -1;
for(int i = 0; i < or.length; i++)
{
if(Character.isDigit(or[i]))
{
returned = Integer.parseInt(Character.toString(or[i]));
or[i] = (char) (returned + 2080);
returned = -1;
}
}
return new String(or);
}
You probably are thinking, well, the code looks clean and should display correctly. However, the part (char) (returned+2080)
doesn't display the symbol - it displays a blank box. I tried to fix it by setting a compatible font (GNU Unifont), but that didn't do anything. Any ideas?
Upvotes: 0
Views: 482
Reputation: 7054
2083 is a hex value, not decimal. See the unicode page for details about this character. I think the value you want is 0x2083, or 8323
Upvotes: 1