Share on Google+Share on Google+

Determining the type of Character

Advertisement
In this section, you will learn how to determine the type of a character.

Determining the type of Character

In this section, you will learn how to determine the type of a character.

You can use the methods of Character class to determine the properties of a character. This class provides several methods for determining a character's category and these methods work for the entire Unicode character set.

In the given example, we have specified a character to define the kind of value contained in a char, whether it is a digit, letter, white space character, space character, or it should be in upper case or in lower case. To determine the character type we have used some static method of Character class:

isLetter(): This method determines if the specified character is a letter.
isDigit(): This method determines if the specified character is a digit.
isLowerCase(): This method determines if the specified character is a lowercase character.
isUpperCase(): This method determines if the specified character is an uppercase character.
isWhitespace(): This method determines if the specified character is white space.
isSpaceChar(): This method determines if the specified character is a Unicode space character.

Here is the code:

public class TypeOfCharacter {
	public static void main(String[] args) {
		char ch = 'a';
		if (Character.isLetter(ch)) {
			System.out.println("Character is a letter");
		}
		if (Character.isDigit(ch)) {
			System.out.println("Character is a digit");
		}
		if (Character.isLowerCase(ch)) {
			System.out.println("Character is in lowercase");
		}
		if (Character.isUpperCase(ch)) {
			System.out.println("Character is in uppercase");
		}
		if (Character.isWhitespace(ch)) {
			System.out.println("Character is a whitespace character");
		}
		if (Character.isSpaceChar(ch)) {
			System.out.println("Character is a space character");
		}
	}

}

Output:

Character is a letter
Character is in lowercase

Advertisements

Advertisement

Posted on: February 16, 2005 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.