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]
# 1
Can you just subtract them and check if the absolute difference is one?
zadoka at 2007-7-9 6:01:07 > top of Java-index,Java Essentials,Java Programming...
# 2

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...

einata at 2007-7-9 6:01:07 > top of Java-index,Java Essentials,Java Programming...
# 3

> > 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.

zadoka at 2007-7-9 6:01:07 > top of Java-index,Java Essentials,Java Programming...
# 4

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");

OleVVa at 2007-7-9 6:01:07 > top of Java-index,Java Essentials,Java Programming...
# 5

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.... :-(

einata at 2007-7-9 6:01:07 > top of Java-index,Java Essentials,Java Programming...
# 6
Mysterious.My code works. It prints "consecutive". What does your code look like? Have you removed the call to getNumericValue() yet?
OleVVa at 2007-7-9 6:01:07 > top of Java-index,Java Essentials,Java Programming...
# 7
Well, I finally managed to do it:if((a+1) == b){...(My first attempt was probably worng...)thanks for the help and sorry...
einata at 2007-7-9 6:01:07 > top of Java-index,Java Essentials,Java Programming...
# 8
Congrats. It's OK, no problem.OleMessage was edited by: OleVV
OleVVa at 2007-7-9 6:01:07 > top of Java-index,Java Essentials,Java Programming...