Home Tutorial Java Corejava Javatext Determining the type of Character

 
 

Determining the type of Character
Posted on: February 16, 2005 at 12:00 AM
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

Related Tags for Determining the type of Character:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.