Checking Lowercase Characters in Java

JavaJavaBeginner
Practice Now

Introduction

The isLowerCase(char ch) method is a part of the Character class in Java. This method is used to check whether the specified character is a lowercase letter or not. This method returns a boolean value indicating whether the specified character is a lowercase letter or not.

Write code to use isLowerCase(char ch) method

In this step, we will write code to use the isLowerCase(char ch) method on different characters. We will define some characters and check whether they are lowercase characters or not.

public class CharacterExample {
  public static void main(String[] args) {
    char ch1 = 'q';
    char ch2 = 'D';
    char ch3 = '8';
    char ch4  = 'w';
    char ch5  = '%';

    boolean b1 = Character.isLowerCase(ch1);
    boolean b2 = Character.isLowerCase(ch2);
    boolean b3 = Character.isLowerCase(ch3);
    boolean b4 = Character.isLowerCase(ch4);
    boolean b5 = Character.isLowerCase(ch5);

    System.out.println(ch1 +" is a lowercase character??:  "+b1);
    System.out.println(ch2 +" is a lowercase character??:  "+b2);
    System.out.println(ch3 +" is a lowercase character??:  "+b3);
    System.out.println(ch4 +" is a lowercase character?? : "+b4);
    System.out.println(ch5 +" is a lowercase character??:  "+b5);
  }
}

Compile and Run the code

To compile the code, run the following command in the terminal:

javac CharacterExample.java

After successfully compiling the code, to execute the code, run the following command:

java CharacterExample

The output should be:

q is a lowercase character??: true
D is a lowercase character??: false
8 is a lowercase character??: false
w is a lowercase character?? : true
% is a lowercase character??: false

Use isLowerCase(char ch) method with user input

In this step, we will take user input and use the isLowerCase(char ch) method on user input to check whether the given character is a lowercase character or not.

import java.util.Scanner;

public class CharacterExample {
  public static void main(String[] args) {
    try {
      System.out.print("Enter the character: ");
      Scanner sc = new Scanner(System.in);
      char ch = sc.next().charAt(0);
      boolean b = Character.isLowerCase(ch);
      System.out.println(ch + " is a lowercase?: "+b);
    }
    catch(Exception e) {
      System.out.println("Invalid Input!!");
    }
  }
}

Compile and Run the code

To compile the code, run the following command in the terminal:

javac CharacterExample.java

After successfully compiling the code, to execute the code, run the following command:

java CharacterExample

When you run the above code, it prompts the user to enter a character. After a user enters a character, the code checks whether the entered character is lowercase or not. The output will look something like this:

Enter the character: u
u is a lowercase?: true

If you enter any non-lowercase character, the output will look something like this:

Enter the character: R
R is a lowercase?: false

Summary

In this lab, we learned about the isLowerCase(char ch) method. This method is used to check whether the specified character is a lowercase letter or not. We also learned about its syntax, parameters, and return type. Lastly, we used this method in different examples to get a better understanding of it.

Other Java Tutorials you may like