Check whether the given character is in upper case lower case or non alphabetic character in Java

All the character variables hold an ASCII value for computer usage. This value can be used to check whether the character is an alphabet or not.

Here, in this program, we are given a character and our task is to check whether the given character is an alphabet or not.

Input: Enter the element: R

Output: It is an alphabet.

The above problem can be solved in the following ways:

Method 1: Using if-else statements

Method 2: Using the ternary operator

Method 3: Using ASCII value

Method 4: Using isAlphabetic() method

Let us take a look at each of these methods separately.

Program 1: To Check Whether the Character is Alphabet or Not

In this method, we will use the if-else statement to check whether the given character is an alphabet or not.

Algorithm:

  1. Start.
  2. Declare a variable.
  3. Initialize it.
  4. Use the if-else statement to check whether the given character is an alphabet or not.
  5. Display the result.
  6. Stop.

Below is the code for the same.

The below program demonstrates how to use the if-else statement to check whether the given character is an alphabet or not.

//Java Program to check whether the given character is an alphabet or not import java.util.Scanner; public class CheckAlphabet { // Driver method public static void main(String []args) { Scanner sc=new Scanner(System.in); char ch; //Declare a character System.out.println("Enter the character "); ch=sc.next().charAt(0); //Initialize the character //check whether alphabet or not using if-else statement if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')) { System.out.print(ch+" is an Alphabet "); } else { System.out.print(ch+" is not an Alphabet "); } } }

Enter the character B

B is an Alphabet

Program 2: To Check Whether the Character is Alphabet or Not

In this method, we will use the ternary operator to check whether the given character is an alphabet or not.

Algorithm:

  1. Start.
  2. Declare a variable.
  3. Initialize it.
  4. Use the ternary operator to check whether the given character is an alphabet or not.
  5. Display the result.
  6. Stop.

Below is the code for the same.

The below program demonstrates how to use the ternary operator to check whether the given character is alphabet or not

//Java Program to check whether the given character is an alphabet or not import java.util.Scanner; public class CheckAlphabet { // Driver method public static void main(String []args) { Scanner sc=new Scanner(System.in); char ch; //Declare a character System.out.println("Enter the character "); ch=sc.next().charAt(0); //Initialize the character //check whether alphabet or not using if-else statement String res = (ch>= 'a' && ch<= 'z') || (ch>= 'A' && ch<= 'Z') ? ch+ " is an alphabet." : ch+ " is not an alphabet."; System.out.println(res); } }

Enter the character d

d is an alphabet.

Program 3: To Check Whether the Character is Alphabet or Not

In this method, we will use the ASCII value to check whether the given character is an alphabet or not. ASCII value is represented by integer values between 0 to 127. The ASCII value of lowercase Alphabets are from 97 to 122 and The ASCII value of uppercase Alphabets are from 65 to 90

Algorithm:

  1. Start.
  2. Declare a variable.
  3. Initialize it.
  4. The ASCII value of the entered character is checked.
  5. If it lies between 97 - 122 or 65 - 90 then, it is an alphabet.
  6. Display the result.
  7. Stop.

Below is the code for the same.

The below program demonstrates how to use ASCII value to check whether the given character is an alphabet or not. Firstly, the character is initialized. and then, its value is compared to the required condition. If the condition satisfies then, it is an alphabet else it is not.

//Java Program to check whether the given character is an alphabet or not import java.util.Scanner; public class CheckAlphabet { // Driver method public static void main(String []args) { Scanner sc=new Scanner(System.in); char ch; //Declare a character System.out.println("Enter the character "); ch=sc.next().charAt(0); //Initialize the character //check whether alphabet or not using if-else statement if((ch>=97 && ch<=122)||(ch>=65 && ch<=90)) { System.out.print(ch+" is an Alphabet"); } else { System.out.print(ch+" is not an Alphabet"); } } }

Enter the character 9

9 is not an Alphabet

Program 4: To Check Whether the Character is Alphabet or Not

In this method, we will use isAlphabetic() method to check whether the given character is an alphabet or not.

Algorithm:

  1. Start.
  2. Declare a variable.
  3. Initialize it.
  4. Use the isAlphabetic() method to check whether the given character is an alphabet or not.
  5. Display the result.
  6. Stop.

Below is the code for the same.

The below program demonstrates how to use isAlphabetic() method to check whether the given character is alphabet or not

//Java Program to check whether the given character is an alphabet or not import java.util.Scanner; public class CheckAlphabet { // Driver method public static void main(String []args) { Scanner sc=new Scanner(System.in); char ch; //Declare a character System.out.println("Enter the character "); ch=sc.next().charAt(0); //Initialize the character //check whether alphabet or not using if-else statement if (Character.isAlphabetic(ch)) { System.out.println(ch + " is an alphabet."); } else { System.out.println(ch + " is not an alphabet."); } } }

Enter the character 89

89 is not an alphabet.

public class Alphabet { public static void main(String[] args) { char c = '*'; if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) System.out.println(c + " is an alphabet."); else System.out.println(c + " is not an alphabet."); } }

Output

* is not an alphabet.

In Java, the char variable stores the ASCII value of a character (number between 0 and 127) rather than the character itself.

The ASCII value of lowercase alphabets are from 97 to 122. And, the ASCII value of uppercase alphabets are from 65 to 90. That is, alphabet a is stored as 97 and alphabet z is stored as 122. Similarly, alphabet A is stored as 65 and alphabet Z is stored as 90.

Now, when we compare variable c between 'a' to 'z' and 'A' to 'Z', the variable is compared with the ASCII value of the alphabets 97 to 122 and 65 to 90 respectively.

Since the ASCII value of * does not fall in between the ASCII value of alphabets. Hence, the program outputs * is not an alphabet.

You can also solve the problem using ternary operator in Java.

Example 2: Java Program to Check Alphabet using ternary operator

public class Alphabet { public static void main(String[] args) { char c = 'A'; String output = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ? c + " is an alphabet." : c + " is not an alphabet."; System.out.println(output); } }

Output

A is an alphabet.

In the above program, the if else statement is replaced with ternary operator (? :).

Example 3: Java Program to Check Alphabet using isAlphabetic() Method

class Main { public static void main(String[] args) { // declare a variable char c = 'a'; // checks if c is an alphabet if (Character.isAlphabetic(c)) { System.out.println(c + " is an alphabet."); } else { System.out.println(c + " is not an alphabet."); } } }

Output

a is an alphabet.

In the above example, notice the expression,

Character.isAlphabetic(c)

Here, we have used the isAlphabetic() method of the Character class. It returns true if the specified variable is an alphabet. Hence, the code inside if block is executed.

A loop like this one:

else if (!(Character.isLowerCase(ch))) { for (int i=1; i<password.length(); i++) { ch = password.charAt(i); if (!Character.isLowerCase(ch)) { System.out.println("Invalid password - Must have a Lower Case character."); password = ""; } // end if } //end for }

Has an obvious logical flaw: You enter it if the first character is not lowercase, then test if the second character is not lower case. At that point you throw an error.

Instead, you should do something like this (not full code, just an example):

boolean hasLower = false, hasUpper = false, hasNumber = false, hasSpecial = false; // etc - all the rules for ( ii = 0; ii < password.length(); ii++ ) { ch = password.charAt(ii); // check each rule in turn, with code like this: if Character.isLowerCase(ch) hasLower = true; if Character.isUpperCase(ch) hasUpper = true; // ... etc for all the tests you want to do } if(hasLower && hasUpper && ...) { // password is good } else { // password is bad }

Of course the code snippet you provided, besides the faulty logic, did not have code to test for the other conditions that your "help" option printed out. As was pointed out in one of the other answers, you could consider using regular expressions to help you speed up the process of finding each of these things. For example,

hasNumber : use regex pattern "\d+" for "find at least one digit" hasSpecial : use regex pattern "[!@#$%^&*]+" for "find at least one of these characters"

In code:

hasNumber = password.matches(".*\\d.*"); // "a digit with anything before or after" hasSpecial = password.matches(".*[!@#$%^&*].*"); hasNoNOT = !password.matches(".*NOT.*"); hasNoAND = !password.matches(".*AND.*");

It is possible to combine these things in clever ways - but especially when you are a novice regex user, it is much better to be a little bit "slow and tedious", and get code that works first time (plus you will be able to figure out what you did six months from now).