8 Replies Latest reply: Feb 14, 2007 9:53 AM by 807606 RSS

    Consecutive unicode characters

    807606
      Hi everyone!
      I have an interesting question regarding unicode handling.
      I'd like my method to get two unicode characters in any language and tell me if they are consecutive 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 the first and second Russian letters, I want it to return true, but if it gets the first and third letters - it'll return false.
      That's why the Collator class can't help me: it doesn't tell me the distance between two foreign letters. It just says which one is "bigger".

      Do you have any ideas for me???
        • 1. Re: Consecutive unicode characters
          807606
          Can you just subtract them and check if the absolute difference is one?
          • 2. Re: Consecutive unicode characters
            807606
            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...
            • 3. Re: Consecutive unicode characters
              807606
              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.
              • 4. Re: Consecutive unicode characters
                807606
                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");
                • 5. Re: Consecutive unicode characters
                  807606
                  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.... :-(
                  • 6. Re: Consecutive unicode characters
                    807606
                    Mysterious.

                    My code works. It prints "consecutive". What does your code look like? Have you removed the call to getNumericValue() yet?
                    • 7. Re: Consecutive unicode characters
                      807606
                      Well, I finally managed to do it:
                      if((a+1) == b){...
                      (My first attempt was probably worng...)
                      thanks for the help and sorry...
                      • 8. Re: Consecutive unicode characters
                        807606
                        Congrats. It's OK, no problem.
                        Ole

                        Message was edited by:
                        OleVV