Analyzing Character Lowercase Status

Beginner

Introduction

In Java, Character class provides several useful methods related to analyzing the characteristics of a character. One such method is the isLowerCase(int codePoint) method, which is used to check whether a specified character is a lowercase letter or not. In this lab, we will learn how to use this method in Java.

Create a Java file

Create a new Java file named IsLowerCaseDemo.java in the ~/project directory using the following command.

cd ~/project
touch IsLowerCaseDemo.java

Write the code to test isLowerCase(int codePoint) method

In this step, we'll write the code to test the isLowerCase(int codePoint) method.

import java.util.Scanner;
public class IsLowerCaseDemo{
    public static void main(String[] args){
        //Test 1: calling isLowerCase(int codePoint) for a lowercase character
        int cp1 = 97;
        boolean b1 = Character.isLowerCase(cp1);
        System.out.println((char)cp1 +" is a lowercase??:  "+b1);

        //Test 2: calling isLowerCase(int codePoint) for an uppercase character
        int cp2 = 65;
        boolean b2 = Character.isLowerCase(cp2);
        System.out.println((char)cp2 +" is a lowercase??:  "+b2);

        //Test 3: calling isLowerCase(int codePoint) for a numeric character
        int cp3 = 49;
        boolean b3 = Character.isLowerCase(cp3);
        System.out.println((char)cp3 +" is a lowercase??:  "+b3);

        //Test 4: calling isLowerCase(int codePoint) for a special character
        int cp4 = 42;
        boolean b4 = Character.isLowerCase(cp4);
        System.out.println((char)cp4 +" is a lowercase??:  "+b4);

        //User input test: calling isLowerCase(int codePoint) for user input characters
        try{
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter a Unicode character: ");
            int cp5 = sc.nextInt();
            boolean b5 = Character.isLowerCase(cp5);
            System.out.println((char)cp5 + " is a lowercase?: "+b5);
        } catch(Exception e){
            System.out.println("Invalid input!");
        }
    }
}

Save and Run The File

Save the file and run the following command to compile and execute it.

javac IsLowerCaseDemo.java
java IsLowerCaseDemo

Summary

In this lab, we learned how to use the isLowerCase(int codePoint) method of the Character class in Java to check whether a specified character is a lowercase letter or not. We created a Java code file named IsLowerCaseDemo.java in the ~/project directory and wrote the code to test the method for different scenarios, including user input. Finally, we compiled and executed the code to verify the output.

Other Tutorials you may like