Tuesday, March 27, 2007

SNIPPET:java.lang.Character(2)

SNIPPET:java.lang.Character(2)

The example below is just a play with some methods of the java.lang.Character class. If I had the Unicode document, then maybe we would have delved into surrogate pairs and supplementary characters.

If you want to know the code point and code values of any Unicode character in the BMP or ASCII char values, try Microsoft word , Insert>Symbols, choose the ACII hex or ASCII dec under the from: field.

Enjoy!

package api_package;

public class CharacterObjects {

//we'll play with names a little more

//taking the names of our beloved citizens

//and since they are more than 1, as char arrays

static char[] slimman = {'Y','A','R','\'','D','U','A'};

static char[] fatman = {'A','T','I','K','U'};

public static void main(String[] args) {

//I want to know the code point of U that they

//both have as values. let's pick Yar'dua

System.out.print("The code point of U in both names is: ");

System.out.println(Character.codePointAt(slimman, 5)); //85

//Now how many values would java use to represent U

//we'll check for validity of code point, then from our

if (Character.isValidCodePoint(85)){

//then the no of values of representing U tells us

//if the java letter lies in our ASCII encoding range

if (Character.charCount(85)>1){

System.out.println("Sorry, U is outside the

Plane 0 " + "or BMP range of value");

}else {

System.out.println("Thanks, U is in the ASCII

range");

}

}

//Now everything has a season and we want to know,

//between Atiku and Yar'dua, who'll come first in

//our virtual dictionary

Character forY = 'Y';

Character forA = 'A';

int received = forY.compareTo(forA);

if (received == 0){

System.out.println("They'll both be fighting for Aso

" + "Rock");

}else if(received<0){

System.out.println("Yar'dua takes our dictionary's "

+"first position.");

}else {

System.out.println("Read my lips. It's Atiku's

stool.");

}

System.out.println("What, even with INEC's murder rope?");

//The dictionary just counts code points, sorry.

System.out.println("Not what you thought. Java took numeric

" + "values, that's all. Take heart.");

}

}

No comments: