Convert Character to Lowercase in Java

Beginner

Introduction

In Java programming, the toLowerCase(char ch) method is used to convert the given character argument to lowercase. This method uses case mapping information provided by the Unicode Data file. In this lab, you will learn how to use this method in Java programs.

Create a new Java file

First, you need to create a new Java file in the project directory. Open your terminal and navigate to the project directory. Then create a new file named CharacterToLowerCase.java using the following command:

$ touch CharacterToLowerCase.java

This will create an empty Java file in the project directory.

Write code to implement the toLowerCase(char ch) method

In this step, you will write the code to implement the toLowerCase(char ch) method. The method takes a character as input and returns the corresponding lowercase character if it is an uppercase character, otherwise, it returns the same character.

public class CharacterToLowerCase {
    public static void main(String[] args) {
        // create a character variable
        char ch = 'F';

        // convert the character to lowercase using toLowerCase(char ch) method
        char lowerCaseCh = Character.toLowerCase(ch);

        // print the original and lowercase character
        System.out.println("Original character: " + ch);
        System.out.println("Lowercase character: " + lowerCaseCh);
    }
}

In the above code, we first create a char variable ch and set its value to 'F'. Then, we use the toLowerCase(char ch) method to convert this character to lowercase and store the result in another char variable lowerCaseCh. Finally, we print out both the original and lowercase characters.

Compile and run the Java program

In this step, you will compile and run the Java program. Open your terminal and navigate to the project directory. Then, compile the CharacterToLowerCase.java file using the following command:

$ javac CharacterToLowerCase.java

This will generate a CharacterToLowerCase.class file in the same directory. Now, execute the program using the following command:

$ java CharacterToLowerCase

This will run the program and display the following output:

Original character: F
Lowercase character: f

Take user input to convert to lowercase

In this step, you will enhance the program to take user input to convert to lowercase. Modify the code as below:

import java.util.Scanner;

public class CharacterToLowerCase {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a character: ");
        char ch = scanner.next().charAt(0);

        char lowerCaseCh = Character.toLowerCase(ch);

        System.out.println("Original character: " + ch);
        System.out.println("Lowercase character: " + lowerCaseCh);
    }
}

In the above modified code, we first import the java.util.Scanner package to take user input. Then, we create a Scanner object to read user input from the console. We prompt the user to enter a character and read it using the scanner.next().charAt(0) method. After that, we use the toLowerCase(char ch) method to convert the character to lowercase and print out the original and lowercase characters.

Compile and run the modified code

In this step, compile and run the modified code from the last step. Execute the following commands in your terminal:

$ javac CharacterToLowerCase.java
$ java CharacterToLowerCase

This will compile and run the modified code. When the program prompts you, enter a character at the console and press the return key. The program should output the original and lowercase characters.

Handle invalid input

In the previous step, if the user enters anything other than a single character, the program may throw an exception. Modify the code to display an error message if the user enters invalid input. Rewrite your code in the following way:

import java.util.Scanner;

public class CharacterToLowerCase {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a character: ");

        try {
            char ch = scanner.next().charAt(0);
            char lowerCaseCh = Character.toLowerCase(ch);

            System.out.println("Original character: " + ch);
            System.out.println("Lowercase character: " + lowerCaseCh);
        } catch (Exception e) {
            System.out.println("Invalid input!");
        }
    }
}

In the above modified code, we enclose the user input code in a try-catch block to handle any exceptions. If an exception is thrown due to invalid input, the program displays an error message. Otherwise, it converts the input character to lowercase and prints out both the original and lowercase characters.

Compile and run the final code

In this step, compile and run the final code from the previous step. Execute the following commands in your terminal:

$ javac CharacterToLowerCase.java
$ java CharacterToLowerCase

This will compile and run the final code. When the program prompts you, enter a single character at the console and press the return key. If you enter invalid input, the program should display an error message.

Summary

In this lab, you learned how to use the Java toLowerCase(char ch) method to convert a given character to its lowercase equivalent. You also learned how to handle user input and exceptions in Java programs. Now that you have completed this lab, you can apply this knowledge to your future Java programming projects.

Other Tutorials you may like