How To Convert Lowercase To Uppercase In Java

Many times programmers have to convert data given by users to be converted into uppercase to avoid language-related confusion. Some keywords in SQL and other languages are recognized in lowercase. To avoid confusion with keywords, data is taken in uppercase. Hence, to convert lowercase to uppercase in programming, you need to use the appropriate string manipulation functions or methods provided by your programming language. How to convert lowercase to uppercase in java is the question that arises to deal with the same situation.

Many times whenever a user fills online forms, lowercase is automatically converted into uppercase to avoid further errors. Also, uppercase help to distinguish the letters easily and they are more legible for reading purposes.

In Java, you can easily convert lowercase characters to uppercase characters using built-in methods provided by the String class. The process of converting lowercase characters to uppercase characters involves changing the ASCII value of each character in the string. The answer to the question – How to convert lowercase to uppercase in java is given below.

Why Is There A Need To Convert Lowercase Into Uppercase?

Converting lowercase characters to uppercase characters in programming can be an essential task while storing the data in a specific format. Let us discuss some points for the same.

Easy to read: It is scientifically proven and can be experienced by an individual that uppercase characters are easy to recognize for reading purposes. A mix of uppercase and lowercase characters are difficult and can create confusion. Converting everything to uppercase can make it easier to read and understand.

Sorting: Uppercase are sorted before lowercase characters. Converting everything to uppercase can make it easier to sort text in alphabetical order.

Validation: Sometimes, you need to validate user input to ensure that it meets certain criteria. For example, you might require that a user’s name be in uppercase letters. Converting the input to uppercase can help you ensure that it meets this requirement.

Comparisons: When comparing text, uppercase and lowercase characters are treated differently. Converting everything to uppercase can make it easier to compare text and tolerate the confusion created by a mix of uppercase and lowercase.

Display: Uppercase characters are simply easier to read and look better on screen or in print. Converting text to uppercase can help improve the readability of your text.

In a nutshell, converting lowercase characters to uppercase characters can help improve the consistency, readability, and accuracy of your text. How to convert lowercase to uppercase in Java is explained below with the help of sample code, its output, and code explanation.

Different Approaches To Convert Lowercase To Uppercase In Java

In Java, there are three approaches to converting lowercase characters to uppercase characters. All these approaches can convert lowercase to uppercase. Here are some common approaches:

1. Using the toUpperCase() method

2. Using the Character.toUpperCase() method

3. Using ASCII values

Using the above conversions, the programmer can definitely achieve the lower to upper conversion. To get more knowledge about the above-mentioned approaches and to get a better understanding of “how to convert lowercase to uppercase in Java”, sample code and their output with explanation are provided below.

Approach 1 : Convert lowercase to uppercase using the toUpperCase() method

Java provides a built-in function toUpperCase() for strings that can be used to convert all lowercase characters in a string to uppercase characters. This built-in function is an easy and straightforward way to achieve conversion easily.

Sample code :

class Main {
    public static void main(String[] args) {

         // Define the variable with lowercase characters
        String lowercaseString = "hello world";

         // Use toUpperCase() method for the conversion
        String uppercaseString = lowercaseString.toUpperCase();

         // Print the final result
        System.out.println(uppercaseString);
    }
}

Output :

HELLO WORLD

Code explanation :

  1. Create a lowercase string with the value “hello world”.
  2. Use the toUpperCase() method on the lowercase string to convert all lowercase characters to uppercase characters.
  3. Print the value of the uppercase string to the console.

Approach 2 : Convert to uppercase using the Character.toUpperCase() method

toUpperCase() method can also convert lowercase characters to uppercase characters, but it has limitations. This method can convert only one character at a time, Hence we can use a loop to iterate the whole string. Also, it can be a time taking process and require more lines of code comparing the previous method.

Sample code :

public class Main {
    public static void main(String[] args) {

        // create the variable and allow a lowercase character
        char lowercaseChar = 'a';

        // Use Character.toUpperCase() method for the conversion
        char uppercaseChar = Character.toUpperCase(lowercaseChar);

        // Print the final result
        System.out.println(uppercaseChar);
    }
}

Output :

A

Code explanation :

  1. Create a char variable lowercaseChar with the value ‘a’.
  2. Then call the toUpperCase() method on the Character class, passing in lowercaseChar as the argument, to convert the character to uppercase.
  3. Print the value of uppercaseChar to the console.

Approach 3 : Create uppercase data using ASCII values

ASCII values are the values associated with each character and symbol by the international body to make the device communication more efficient. These values can be interchanged to convert the lowercase characters into uppercase.

Here is the explanation of the step and method that is implemented in this approach. The programmer has to create a program that will subtract the ASCII value of character ‘a’ (which is 97) from the ASCII value of the lowercase character taken from the user and add the ASCII value of character ‘A’ (which is 65) to obtain the required result.

Sample code :

public class Main {
    public static void main(String[] args) {
        char lowercaseChar = 'm';
        char uppercaseChar = (char)(lowercaseChar - 32);
        System.out.println(uppercaseChar);
    }
}

Output :

M

Code explanation :

  1. Create a char variable lowercaseChar with the value ‘m’.
  2. Subtract the ASCII value of ‘a’ (97) from the ASCII value of lowercaseChar and add the ASCII value of ‘A’ (65).
  3. Subtract 32 from the ASCII value of the lower character, because 97 – 35 = 32, which is used in the sample code.
  4. Print the value to the console.

Best Approach Out Of Three For Converting Lowercase To Uppercase In Java

Three approaches mentioned earlier for converting lowercase characters to uppercase characters in Java, using the toUpperCase() method is the best approach according to our research. Here are some points to justify the statement.

Predefined built-in method: The toUpperCase() method is a built-in method provided by the String class in Java. This method is more convenient for programmers as they have to use the method directly in their code.

Less line of codes: The toUpperCase() method is a simple and concise way to convert lowercase characters to uppercase characters. It only requires one line of code, which makes it easy to use and understand.

Efficient: The toUpperCase() method is highly optimized and efficient. It uses advanced algorithms to perform the conversion quickly and accurately, making it suitable for use in performance-critical applications.

More useful: This method can convert the whole string into uppercase. All other approaches can be applied to one character only. Hence, it is more useful than others.

Overall, while all three approaches can achieve the same result, using the toUpperCase() method is the preferred approach due to its simplicity, efficiency, and built-in nature.

The approach chosen will ultimately be determined by the application’s specific limitations and objectives, as well as the developer’s preferences and knowledge.

Sample Problems

Sample Problem 1

How to convert lowercase into uppercase in Java using the toUpperCase() method.  Write a Java program that prompts the user to enter his full name and then converts the string to uppercase using the toUpperCase() method. Display the resulting uppercase string to the user.

Sample code :

// Import the scanner library to take user input
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Take the user input
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter your full name: ");
        String a = input.nextLine();
        
        // Convert it into Uppercase
        String b = a.toUpperCase();
        
        // Print the result
        System.out.println("Your name is: " + b);
    }
}

Output :

Enter your full name: hritik roshan
Uppercase string: HRITIK ROSHAN

Code Explanation :

  1. Create a program to take user input.
  2. In the sample code – the user input is “hritik roshan”.
  3. Use the toUpperCase() method on the lowercase string to convert all lowercase characters to uppercase characters.
  4. Obtain the output using a print statement.

Sample Problem 2

How to convert from lowercase to uppercase in Java using the Character.toUpperCase() method. A teacher wanted to know the section of each student. Write a Java program that takes the input from the student and then converts the character to uppercase using the Character.toUpperCase() method. Display the resulting uppercase string to the student.

Sample code :

// Import the scanner library to take user input
import java.util.Scanner;

public class LowercaseToUppercase {
    public static void main(String[] args) {
        // Take the user input
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter your section: ");
        char a = input.next().charAt(0);
        
        // Convert it into Uppercase
        char b = Character.toUpperCase(a);
        
        // Print the result
        System.out.println("Your section is: " + b);
    }
}

Output :

Enter your section: a
Your section is: A

Code Explanation :

  1. Create a char variable lowercaseChar with the value ‘o’.
  2. Then call the toUpperCase() method on the Character class, passing in lowercaseChar as the argument, to convert the character to uppercase.
  3. Obtain the output using a print statement.

Sample Problem 3

How to change lowercase to uppercase in java using ASCII values calculation. Write a Java program that prompts the user to enter the initial letter of his name and then converts the character to uppercase using ASCII values calculation. Display the resulting uppercase string to the user.

Sample code :


// Import the scanner library to take user input
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        
        // Take the user input
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter the initial letter of your name: ");
        char a = input.next().charAt(0);
        
        // Convert it into Uppercase using ASCII conversion
        char b = (char)(a - 32);
        
        // Print the result
        System.out.println("Uppercase character: " + b);
    }
}

Output :

Enter the initial letter of your name: j
Uppercase character: J

Code Explanation :

  1. Take the user input using Scanner class and assign it to variable a.
  2. Subtract 32 from the ASCII value of the lower character, because 97 – 35 = 32, which is used in the sample code.
  3. Obtain the output using a print statement.

Conclusion

Uppercase characters are easy to read and avoid confusion created by the words which are a mix of uppercase and lowercase characters. Hence, on many occasions, programmers have to convert lowercase characters to uppercase characters. Then the question arises – How to change lowercase to uppercase in Java? This conversion can be achieved by three approaches which are mentioned above.

Conversion using the toUpperCase() method is the simplest and most useful approach among all the approaches. It can be applied on a whole string and consume fewer lines of code.

Other approaches are not that useful as they can convert only one character at a time. If the programmer has to convert a whole string then a loop can be applied to the program. But, again it consumes more lines and code, and the code becomes more complex.