Convert Unicode Code Point to Lowercase

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the toLowerCase(int codePoint) method in Java. It is a method of the Character class and is used to convert the specified Unicode code point character argument to its lowercase equivalent. The toLowerCase() method uses the case mapping information provided by the Unicode Data file.

Setting up the project

  1. Open the terminal of Ubuntu system and navigate to the ~/project directory using the cd command.

  2. Create a new file named CharacterDemo.java. You can use the nano command to create and edit the file.

    touch CharacterDemo.java
  3. In the CharacterDemo.java file, create a public class named CharacterDemo.

    public class CharacterDemo {
    
    }

Using the toLowerCase() method to convert individual characters

  1. Inside the CharacterDemo class, create a public static void main method.

    public static void main(String[] args){
    
    }
  2. Declare and initialize an integer variable named cp to any valid Unicode code point value.

    int cp = 67;
  3. Invoke the toLowerCase() method and pass the cp variable as a parameter.

    char lowerCaseChar = Character.toLowerCase((char)cp);
  4. Print the lowercase character to the console.

    System.out.println("Lowercase character: " + lowerCaseChar);
  5. Save the file and exit the editor.

Using the toLowerCase() method in a loop

  1. Inside the main method, create a string variable named inputString and initialize it with any string of your choice.

    String inputString = "HELLO";
  2. Create a character array named charArray and initialize it by converting inputString to a character array using the toCharArray() method.

    char[] charArray = inputString.toCharArray();
  3. Use a for loop to iterate through each character in the charArray.

    for(int i=0; i<charArray.length; i++){
    
    }
  4. Inside the for loop, invoke the toLowerCase() method and pass the current character of the charArray as a parameter.

    char lowercaseChar = Character.toLowerCase(charArray[i]);
  5. Print the lowercase character to the console.

    System.out.print(lowercaseChar);
  6. Save the file and exit the editor.

Using user input to get Unicode code point value

  1. Inside the main method, create an instance of the Scanner class to get user input.

    Scanner scanner = new Scanner(System.in);
  2. Output a message to the user to enter a Unicode code point value.

    System.out.println("Enter Unicode code point value:");
  3. Use the nextInt() method of the Scanner class to get the user input as an integer.

    int codePoint = scanner.nextInt();
  4. Use the toLowerCase() method and pass the codePoint as a parameter to convert the code point value to its lowercase equivalent.

    char lowercaseChar = Character.toLowerCase((char) codePoint);
  5. Print the lowercase character to the console.

    System.out.println("Lowercase character: " + lowercaseChar);
  6. Save the file and exit the editor.

Compiling and Running the program

  1. Compile the CharacterDemo.java file using the javac command.

    javac CharacterDemo.java
  2. Run the program using the java command.

    java CharacterDemo
  3. Enter the Unicode code point values and observe the output of the program.

  4. Congratulations! You have successfully completed the lab.

Summary

In this lab, we learned how to use the toLowerCase(int codePoint) method in Java to convert Unicode code point values to their lowercase equivalents. We also learned how to use the Scanner class to get user input and how to iterate over an array of characters using a for loop.

Other Java Tutorials you may like