Uppercase characters and lowercase characters are the basic formats for representing data. In programming, data stored in the application required conversion into either lowercase or uppercase as per the data representation requirements.
Converting uppercase characters into lowercase can be the requirement of the system to represent the data or the readability of data. Lowercase characters are easy to identify when comparing the lowercase and uppercase characters.
In this article, we will discuss the real-life requirement and application of the conversion through detailed explanations and different approaches.
Why Is There A Need To Convert Uppercase To Lowercase In Java
Now, let us discuss the application of conversion in real life. Other scenarios can also be considered while developing an application.
- Uniformness: Many times a programmer has to represent the data in a uniform way such as uppercase or lowercase characters. To represent the data in a uniform format, it can be converted into lowercase.
- String process: Whenever a programmer works on string data, it should be processed in the specific pattern to carry out string operation. To achieve the specific pattern it can be converted into lowercase.
- Output: Many times, the output of the data should be displayed in lowercase. To fulfill this requirement, data is converted into lowercase. Hence, this conversion becomes essential for the developer.
Different Approaches To Convert Uppercase To Lowercase In Java
Java provides different built-in libraries and methods to convert upper case characters into lowercase ones. These approaches are highly professional and accurate as they use advanced algorithms to achieve conversion.
- Using the toLowerCase() method
- Using the Character.toLowerCase() method
- Using ASCII code values
- Using a lookup table
- Using Guava Library
Let’s discuss the above approaches through a proper explanation, code, and their implementation.
Approach 1 : Convert uppercase to lowercase using the toLowerCase() method
In Java, strings can be manipulated through many methods. The toLowerCase() method is the in-built method and has a simple syntax. It is also beginner friendly.
Sample code :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// take the user input for the conversion
Scanner input = new Scanner(System.in);
System.out.print("Enter a string in uppercase: ");
String uppercaseString = input.nextLine();
// use the method for the conversion
String lowercaseString = uppercaseString.toLowerCase();
// print the output on the console
System.out.println("Lowercase string: " + lowercaseString);
input.close();
}
}
Output :
Enter a string in uppercase: KETAN
Lowercase string: ketan
Explanation :
- First take string input in uppercase
- Then, apply the toLowerCase() method to convert the given string
- Print the output on the console
Approach 2 : Convert uppercase to lowercase the Character.toLowerCase() method
StringBuilder class is another utility that can manipulate the strings and get the desired results. The Character.toLowerCase() method is the in-built method in the StringBuilder class which converts Uppercase characters into Lowercase.
Sample code :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// take the user input for the conversion
Scanner input = new Scanner(System.in);
System.out.print("Enter a string in uppercase: ");
String uppercaseString = input.nextLine();
// create the object of StringBuilder class and use the method for the conversion using the loop
StringBuilder lowercaseString = new StringBuilder();
for (int i = 0; i < uppercaseString.length(); i++) {
char c = uppercaseString.charAt(i);
if (Character.isUpperCase(c)) {
c = Character.toLowerCase(c);
}
lowercaseString.append(c);
}
// print the output on the console
System.out.println("Lowercase string: " + lowercaseString.toString());
input.close();
}
}
Output :
Enter a string in uppercase: HARSHIT
Lowercase string: harshit
Explanation :
- First take string input in uppercase
- Create the object of StringBuilder class
- Then, apply the Character.toLowerCase() method to convert the given string
- Print the output on the console
Approach 3 : Convert uppercase to lowercase using the ASCII code values
ASCII code values are international standard values associated with uppercase characters, lowercase characters, special symbols, and others. These standard values can be used to convert uppercase characters into lowercase ones. This method involves a few arithmetic calculations.
Sample code :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// take the string input for the conversion
Scanner input = new Scanner(System.in);
System.out.print("Enter a string in uppercase: ");
String uppercaseString = input.nextLine();
// create the object of StringBuilder class and use the ASCII values method for the conversion using the loop
StringBuilder lowercaseString = new StringBuilder();
for (int i = 0; i < uppercaseString.length(); i++) {
char c = uppercaseString.charAt(i);
if (c >= 'A' && c <= 'Z') {
// Convert uppercase to lowercase using ASCII code values
c = (char) (c + 32);
}
lowercaseString.append(c);
}
// print the output on the console
System.out.println("Lowercase string: " + lowercaseString.toString());
input.close();
}
}
Output :
Enter a string in uppercase: PARV
Lowercase string: parv
Explanation :
- First take string input in uppercase
- Create the object of StringBuilder class
- Then, apply the ASCII value algorithm
- Add the 32 to each uppercase character and the resulting value will represent the lowercase of the character
- Print the output on the console
Approach 4 : Convert uppercase to lowercase using the lookup table
Lookup is the mathematical expression used to compare and replace the values. In Java, the lookup table can be used to compare the input data and convert it into a desirable format to display the expected output.
Sample code :
import java.util.Scanner;
public class UppercaseToLowercase3 {
public static void main(String[] args) {
// user input for the conversion
Scanner input = new Scanner(System.in);
System.out.print("Enter a string in uppercase: ");
String uppercaseString = input.nextLine();
// map the uppercase characters into lowercase characters using loop
char[] lookupTable = new char[26];
for (int i = 0; i < 26; i++) {
lookupTable[i] = (char) ('a' + i);
}
// create the object of StringBuilder class and use the method using the loop
StringBuilder lowercaseString = new StringBuilder();
for (int i = 0; i < uppercaseString.length(); i++) {
char c = uppercaseString.charAt(i);
if (c >= 'A' && c <= 'Z') {
c = lookupTable[c - 'A'];
}
lowercaseString.append(c);
}
// Print the final result
System.out.println("Lowercase string: " + lowercaseString.toString());
input.close();
}
}
Output :
Enter a string in uppercase: NAMAN
Lowercase string: naman
Explanation :
- Take the user input in uppercase to carry out the conversion
- Then use the lookup table to map the uppercase characters into lowercase
- Then use the StringBuilder class to achieve the conversion
- Finally, print the output
Approach 5 : Convert uppercase to lowercase using the Guava library
Guava is the open-source library that provides prewritten code for some common Java programs.
The CaseFormat.UPPER_UNDERSCORE.to() method in Guava is used to convert the uppercase characters into lowercase.
To run this code we have to install an exclusive guava library and set the environment variable. This library is not supported by online compilers.
Sample code :
// import the necessary libraries
import com.google.common.base.CaseFormat;
import java.util.Scanner;
public class UppercaseToLowercaseGuava {
public static void main(String[] args) {
// take the string input for the conversion
Scanner input = new Scanner(System.in);
System.out.print("Enter a string in uppercase: ");
String uppercaseString = input.nextLine();
// use the com.google.common.base.CaseFormat class and its method for the conversion
String lowercaseString = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, uppercaseString);
// Print the final result
System.out.println("Lowercase string: " + lowercaseString);
input.close();
}
}
Output :
Enter a string in uppercase: LAKSHYA
Lowercase string: lakshya
Explanation :
- Import the libraries including com.google.common.base.Splitter.
- Take the user input in the string format.
- Using the com.google.common.base.CaseFormat class, to apply the method.
- Then, use CaseFormat.UPPER_UNDERSCORE.to() method to convert the uppercase into lowercase.
- Finally, print the output on the console.
Best Approach Out Of Five for Converting Uppercase To Lowercase In Java
As per our research, the best approach is to use the toLowerCase() method. Here are the reasons to choose this –
- Beginner friendly- The toLowerCase() method is easy to understand and doesn’t have any advanced algorithm involved.
- Simple syntax- The toLowerCase() method has a predefined syntax that is easy to apply in the code by passing appropriate parameters.
- Convert in one go- In the toLowerCase() method looping is not required as it can convert the whole string in one go.
Sample Problems
Sample problem 1:
While filling out the survey form, the E-mail ID is taken from the participant. Create the Java program to get the mail from the user and convert it into lowercase using the toLowerCase() method.
Solution :
First take the email input from the survey participant
Then, apply the toLowerCase() method to convert the given string
Print the output on the console
Code :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// take the user email for the conversion
Scanner input = new Scanner(System.in);
System.out.print("Enter your email: ");
String uppercaseString = input.nextLine();
// use the method for the conversion
String lowercaseString = uppercaseString.toLowerCase();
// print the output on the console
System.out.println("Your email is: " + lowercaseString);
input.close();
}
}
Output :
Enter your email: [email protected]
Your email is: [email protected]
Sample problem 2:
Take the feedback from the audience who attained the regional program. Convert the feedback into lowercase using the Character.toLowerCase() method.
Solution :
First take the feedback from any particular audience
Create the object of StringBuilder class
Then, apply the Character.toLowerCase() method to convert the given string Print the output on the console
Code :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// take the user feedback string for the conversion
Scanner input = new Scanner(System.in);
System.out.print("Enter your feedback: ");
String uppercaseString = input.nextLine();
// create the object of StringBuilder class and use the method for the conversion using the loop
StringBuilder lowercaseString = new StringBuilder();
for (int i = 0; i < uppercaseString.length(); i++) {
char c = uppercaseString.charAt(i);
if (Character.isUpperCase(c)) {
c = Character.toLowerCase(c);
}
lowercaseString.append(c);
}
// print the output on the console
System.out.println("Feedback entered is: " + lowercaseString.toString());
input.close();
}
}
Output :
Enter your feedback: WELL-ORGANIZED PROGRAM AND CAN BE ORGANIZED EACH SUNDAY.
Feedback entered is: well-organized program and can be organized each sunday.
Sample problem 3:
Get suggestions from the company investors to expand the profits of the company. Convert the suggestions into lowercase through ASCII code values and display on the console.
Solution :
- First take the suggestion from the investor as input
- Create the object of StringBuilder class
- Then, apply the ASCII value algorithm
- Add the 32 to each uppercase character and the resulting value will represent the lowercase of the character
- Print the output on the console
Code :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// take the suggestion from the investor
Scanner input = new Scanner(System.in);
System.out.print("Enter your suggestion: ");
String uppercaseString = input.nextLine();
// create the object of StringBuilder class and use the ASCII values method for the conversion using the loop
StringBuilder lowercaseString = new StringBuilder();
for (int i = 0; i < uppercaseString.length(); i++) {
char c = uppercaseString.charAt(i);
if (c >= 'A' && c <= 'Z') {
// Convert uppercase to lowercase using ASCII code values
c = (char) (c + 32);
}
lowercaseString.append(c);
}
// print the output on the console
System.out.println("Suggestion is: " + lowercaseString.toString());
input.close();
}
}
Output :
Enter your suggestion: WE SHOULD ORGANIZE A SOCIAL MEDIA CAMPAIGN
Suggestion is: we should organize a social media campaign
Sample problem 4:
Create a program for the cyber cafe to ask the user for registering himself and ask for which work he wants to use the internet connection in the cyber cafe. Convert the string into lowercase using the lookup table and then print the result.
Solution :
- Take the user input for the work he had come in the cyber cafe
- Then use the lookup table to map the uppercase characters into lowercase
- Then use the StringBuilder class to achieve the conversion
- Finally, print the output
Code :
import java.util.Scanner;
public class UppercaseToLowercase3 {
public static void main(String[] args) {
// take the user input for the conversion
Scanner input = new Scanner(System.in);
System.out.print("Enter the work: ");
String uppercaseString = input.nextLine();
// map the uppercase characters into lowercase characters using loop
char[] lookupTable = new char[26];
for (int i = 0; i < 26; i++) {
lookupTable[i] = (char) ('a' + i);
}
// create the object of StringBuilder class and use the method for the conversion using the loop
StringBuilder lowercaseString = new StringBuilder();
for (int i = 0; i < uppercaseString.length(); i++) {
char c = uppercaseString.charAt(i);
if (c >= 'A' && c <= 'Z') {
c = lookupTable[c - 'A'];
}
lowercaseString.append(c);
}
// Print the final result
System.out.println("You want to: " + lowercaseString.toString());
input.close();
}
}
Output :
Enter the work: FILLING THE SCHOLARSHIP FORM
You want to: filling the scholarship form
Sample problem 5:
Create a Java program that asks the students for their hobbies and print them in lowercase using the Guava library.
Solution :
- Import the libraries including com.google.common.base.Splitter.
- Take the information from the user.
- Using the com.google.common.base.CaseFormat class, to apply the method.
- Then, use CaseFormat.UPPER_UNDERSCORE.to() method to convert the uppercase into lowercase.
- Finally, print the output on the console.
Code :
// import the necessary libraries
import com.google.common.base.CaseFormat;
import java.util.Scanner;
public class UppercaseToLowercaseGuava {
public static void main(String[] args) {
// ask students to enter their hobbies
Scanner input = new Scanner(System.in);
System.out.print("Enter your hobby/hobbies: ");
String uppercaseString = input.nextLine();
// use the com.google.common.base.CaseFormat class and its method for the conversion
String lowercaseString = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, uppercaseString);
// Print the final result
System.out.println("You like to: " + lowercaseString);
input.close();
}
}
Output :
Enter your hobby/hobbies: READING NON-FICTION AND SKATING
You like to: reading non-fiction and skating
Conclusion
As we have already discussed, the conversion of the uppercase characters into lowercase characters can be an essential task for the programmer if he or she wants the desired outputs.
We have discussed five different approaches to converting uppercase characters into lowercase in Java.
Also, we have considered the toLowerCase() method as the best approach for its simplicity and ease of code and understanding. But, to become a better programmer, one should try all the approaches and deeply understand them.