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
Open the terminal of Ubuntu system and navigate to the
~/projectdirectory using thecdcommand.Create a new file named
CharacterDemo.java. You can use thenanocommand to create and edit the file.touch CharacterDemo.javaIn the
CharacterDemo.javafile, create a public class namedCharacterDemo.public class CharacterDemo { }
Using the toLowerCase() method to convert individual characters
Inside the
CharacterDemoclass, create a public static void main method.public static void main(String[] args){ }Declare and initialize an integer variable named
cpto any valid Unicode code point value.int cp = 67;Invoke the
toLowerCase()method and pass thecpvariable as a parameter.char lowerCaseChar = Character.toLowerCase((char)cp);Print the lowercase character to the console.
System.out.println("Lowercase character: " + lowerCaseChar);Save the file and exit the editor.
Using the toLowerCase() method in a loop
Inside the
mainmethod, create a string variable namedinputStringand initialize it with any string of your choice.String inputString = "HELLO";Create a character array named
charArrayand initialize it by convertinginputStringto a character array using thetoCharArray()method.char[] charArray = inputString.toCharArray();Use a for loop to iterate through each character in the
charArray.for(int i=0; i<charArray.length; i++){ }Inside the for loop, invoke the
toLowerCase()method and pass the current character of thecharArrayas a parameter.char lowercaseChar = Character.toLowerCase(charArray[i]);Print the lowercase character to the console.
System.out.print(lowercaseChar);Save the file and exit the editor.
Using user input to get Unicode code point value
Inside the
mainmethod, create an instance of theScannerclass to get user input.Scanner scanner = new Scanner(System.in);Output a message to the user to enter a Unicode code point value.
System.out.println("Enter Unicode code point value:");Use the
nextInt()method of theScannerclass to get the user input as an integer.int codePoint = scanner.nextInt();Use the
toLowerCase()method and pass thecodePointas a parameter to convert the code point value to its lowercase equivalent.char lowercaseChar = Character.toLowerCase((char) codePoint);Print the lowercase character to the console.
System.out.println("Lowercase character: " + lowercaseChar);Save the file and exit the editor.
Compiling and Running the program
Compile the
CharacterDemo.javafile using thejavaccommand.javac CharacterDemo.javaRun the program using the
javacommand.java CharacterDemoEnter the Unicode code point values and observe the output of the program.
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.



