Consecutive unicode characters
Hi everyone!
I have an interesting question regarding unicode handling.
I'd like my method to get two unicode charactersin any language and tell me if they areconsecutive ascending letters or not.
I mean: I want to get two foreign letters and check if they come one after the other. For example: If my method gets thefirst andsecond Russian letters, I want it to returntrue, but if it gets thefirst andthird letters - it'll returnfalse.
That's why theCollator class can't help me: it doesn't tell me thedistance between two foreign letters. It just says which one is "bigger".
Do you have any ideas for me?
[733 byte] By [
einata] at [2007-11-26 18:27:01]

char a;
char b;
int numericVal1 = Character.getNumericValue(a);
int numericVal2 = Character.getNumericValue(b);
if((numericVal1+1) == numericVal2){
throw....}
This is my code. For Russian letters, the getNumericValue returns -1...
I also tried substructing, but got 0 for 2 consecutive letters...
> > char a;
> char b;
> int numericVal1 = Character.getNumericValue(a);
> int numericVal2 =
> Character.getNumericValue(b);
> if((numericVal1+1) ==
> numericVal2){
> hrow....}
> This is my code. For Russian letters, the
> getNumericValue returns -1...
> I also tried substructing, but got 0 for 2
> consecutive letters...
1. Read through the API about the getNumericValue() method. Especially the part talking about returning -1
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html
2. The following code will only work if the numericVal1 comes before val2:
if((numericVal1+1) == numericVal2){
What happens if Val1 is 50 and Val2 is 49, that is a match that your code doesn't catch. Instead, subtract and determine if the absolute of the difference is one.
Don't use getNumericValue(). It returns 1 for the character '1', 2 for '2' etc., but nothing you can use for most unicode characters, as you've noticed. Just pretend the character is already a number:
char a = '\u0034';
char b = '\u0035';
if (b - a == 1) System.out.println("consecutive");
else System.out.println("not consecutive");
I'm actually looking for ascending consecutive letters, so the opposite case (numericVal2 comes before numericVal1) is irrelevant for me.
Also, as I wrote - substraction doesn't help me, as these are unicode values. I only get 0 when I try to substract two foreign letters.... :-(