Determining Letter Characters with isLetter() Method in Java: A Comprehensive Guide
Table of Content:
Description
A character is considered to be a letter if its general category type, provided by Character.getType(ch), is any of the following:
- UPPERCASE_LETTER
- LOWERCASE_LETTER
- TITLECASE_LETTER
- MODIFIER_LETTER
- OTHER_LETTER
Not all letters have case. Many characters are letters but are neither uppercase nor lowercase nor titlecase.
This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the isLetter(int) method.
The isLetter(char ch) method of Character class is static thus it should be accessed statically which means the we would be calling this method in this format:
Character.isLetter(char ch)
Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java isLetter() method non statically.
The method determines whether the specified char value is a letter.
Method Syntax
boolean isLetter(char ch)
Method Argument
Data Type | Parameter | Description |
---|---|---|
char | ch | the character to be tested. |
Method Returns
This method returns true if the passed character is really a character.
Compatibility
Requires Java 1.0 and up
Example
Below is a simple java example on the usage of isLetter(char ch) method of Character class.
public class MethodisLetter { public static void main(String args[]) { System.out.println(Character.isLetter('s')); System.out.println(Character.isLetter('2')); } }
output
This will produce the following result ?
true false Press any key to continue . . .
Example
Below is a simple java example on the usage of isLetter(char ch) method of Character class.
import java.util.Scanner; /* * This example source code demonstrates the use of * isLetter(char ch) method of Character class. */ public class CharacterIsLetterChar { public static void main(String[] args) { // Ask for user input System.out.print("Enter a character:"); // use scanner to get the user input Scanner s = new Scanner(System.in); // get a single character char value = s.nextLine().toCharArray()[0]; // close the scanner object s.close(); // check if the user input is a letter or not boolean checkBool = Character.isLetter(value); // print result if(checkBool){ System.out.println("User input \'"+value+"\' is a letter"); } else{ System.out.println("User input \'"+value+"\' is not a letter"); } } }
output
This will produce the following result ?
Enter a character:s User input 's' is a letter Press any key to continue . . . Enter a character:6 User input '6' is not a letter Press any key to continue . . .