DEV Community

Lalit Kumar
Lalit Kumar

Posted on

How to test if a char is a letter in java?

In this article, I will explain how to find whether a given character is a letter or not by using isLetter(). I have stated an example for your reference for your understanding.

title: "How to test if a char is a letter in java?"
tags: java

canonical_url: https://kodlogs.com/blog/2628/how-to-test-if-a-char-is-a-letter-in-java

Approach

  • I have used a pre-defined class which is used to take values at run-time. This Scanner class is available in the util package.
  • I have taken a character input ch and checked whether it is a character or not. It is as simple as it is said.
  • Refer to the below example and find out how to check a char if it is a letter or not. ##Program
import java.util.Scanner;

public class Demo

{

public static void main(String[] args)

{

Scanner  scanner = new Scanner(System.in);

System.out.println(“ Input a character “);

String str = scanner.next();

char ch = str.charAt(0);

boolean b = Character.isLetter(ch);

if(ch)

{

System.out.println(ch + “is a character”);

}

else

{

System.out.println(ch + “ is not a character”);

}

}
}
Enter fullscreen mode Exit fullscreen mode

Output

`
Input a character

4

4 is not a character

Input a character

J

J is a character
`

Top comments (0)