In Java, to check if a character is uppercase or lowercase, we can use either an if-else condition or the ternary operator. This article focuses on learning through examples, algorithms, detailed logic, and program explanations for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements
- Ternary Operator
Problem Statement
We need to write a Java program to check if a given character is uppercase or lowercase. The program will take a character as input and print whether it is in uppercase or lowercase. This above action should be done in our program. Let’s look at an example,
Example 1:
Input: C Output: Given character is in uppercase
Here, for the input C, Output is Output is “Given character is in uppercase”
Example 2:
Input: c Output: Given character is in lowercase
Here, for the input c, Output is Output is “Given character is in lowercase”
Algorithm to check character is in Uppercase or Lowercase
- Start: Begin the program with character input ch
- We use next() method to read and extract the first character using charAt(0).
- Check Condition:
- If alphabets greater than or equal to ‘A’ and less than or equal to ‘Z’ , we say they are in uppercase
- ch>=’A’ && ch<=’Z’ , alphabets are in uppercase
- If alphabets greater than or equal to ‘a’ and less than or equal to ‘z’ , we say they are in lowercase
- ch>=’a’ && ch<=’z’ , alphabets are in lowercase
- If alphabets greater than or equal to ‘A’ and less than or equal to ‘Z’ , we say they are in uppercase
Logic to check if an character is in Uppercase or Lowercase
There are multiple logics to check a given input character is in uppercase or lowercase in Java Programing language.
Logic 1: Using if-else
- Here, we are checking if a character (alphabet) is in uppercase or lowercase.
- If the character is greater than or equal to ‘A’ and less than or equal to ‘Z’, we say it is in uppercase.
- Otherwise, if the character is greater than or equal to ‘a’ and less than or equal to ‘z’, we say it is in lowercase.
if (ch >= 'A' && ch <= 'Z')
System.out.println(ch + " is Uppercase");
else if (ch >= 'a' && ch <= 'z')
System.out.println(ch + " is Lowercase");
else
System.out.println(ch+ " is not an alphabet");
Logic 2: Ternary Operator
- This operator takes three operands and functions similarly to if-else statements. If the condition is true, it evaluates the first expression, if false, it evaluates the second expression.
- For alphabets:
- If a character is between ‘A’ and ‘Z’, it is considered uppercase.
- Otherwise, if it is between ‘a’ and ‘z’, it is considered lowercase.
String result;
result =(ch >= 'A' && ch <= 'Z')?
ch + " is Uppercase":
(ch >= 'a' && ch <= 'z')?
ch + " is Lowercase":
ch+ " is not an alphabet";
System.out.println(result);
Java Program to Check Character is Uppercase or Lowercase
Program 1: Using if-else
In the below program we are using if-else to check given character is uppercase or lowercase in Java.
import java.util.Scanner;
public class UppperCaseLowerCaseCheck {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a character");
char ch = s.next().charAt(0);
if (ch >= 'A' && ch <= 'Z')
System.out.println(ch + " is Uppercase");
else if (ch >= 'a' && ch <= 'z')
System.out.println(ch + " is Lowercase");
else
System.out.println(ch+ " is not an alphabet");
s.close();
}
}
Output:
Enter a character
A
A is Uppercase
Java program explanation to check given character is Uppercase or Lowercase
- The program imports the
Scanner
class fromjava.util
package to facilitate user input. - Program defines a class named
UppperCaseLowerCaseCheck
. - It contains the main method where the program execution starts.
- Asks the user to enter a character.
- Reads the user input character using
Scanner
snext()
method and extracts the first character usingcharAt(0)
. - It checks if the input character falls within the range of uppercase letters (‘A’ to ‘Z’) using ASCII values.
- If the character is within this range, it prints that the character is uppercase.
- Otherwise, it checks if the character falls within the range of lowercase letters (‘a’ to ‘z’) using ASCII values.
- If the character is within this range, it prints that the character is lowercase.
- When neither condition is met, it prints that the character is not an alphabet.
- It closes the
Scanner
objects
to release system resources.
Program 2: Using Ternary Operator
In the below program we are using Ternary Operator to check given character is in uppercase or lowercase in Java.
import java.util.Scanner;
public class UppperCaseLowerCaseCheck {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a character");
char ch = s.next().charAt(0);
String result;
result =(ch >= 'A' && ch <= 'Z')?
ch + " is Uppercase":
(ch >= 'a' && ch <= 'z')?
ch + " is Lowercase":
ch+ " is not an alphabet";
System.out.println(result);
s.close();
}
}
Output:
Enter a character
c
c is Lowercase
Java program explanation to check given character is Uppercase or Lowercase
- The program imports the
Scanner
class fromjava.util
package to facilitate user input. - Then it defines a class named
UppperCaseLowerCaseCheck
. - It contains the main method where the program execution starts.
- Asks the user to enter a character.
- Reads the user input character using
Scanner
‘snext()
method and extracts the first character usingcharAt(0)
. - It uses nested ternary operators to determine whether the input character is uppercase, lowercase, or not an alphabet.
- If the character is within the range of uppercase letters (‘A’ to ‘Z’) using ASCII values, it assigns a message indicating that the character is uppercase.
- Otherwise, if the character is within the range of lowercase letters (‘a’ to ‘z’) using ASCII values, it assigns a message indicating that the character is lowercase.
- If neither condition is met, it assigns a message indicating that the character is not an alphabet.
- It prints the message indicating the result obtained from the ternary operator evaluation.
- Program closes the
Scanner
objects
to release system resources.
Conclusion
In this article, we explored how to check if a character is in uppercase or lowercase using using if-else and the ternary operator. We understood the logic and proceed to implement the program.
For more related topics in Java, you might find these articles helpful:
- Understanding the concepts of Conditional statements in Java
- Top 10 Java Programming tips
- Concepts of Operators in Python
We hope you find this article helpful. If you have any questions or need clarification, please leave a comment below. Don’t forget to subscribe to our newsletter for more updates on Java programming tips and tutorials.
Happy Java Programming!