How To Convert String To Uppercase In Java

Whenever a programmer takes user input, it is generally stored in string data type. The string data type is a mix of characters or a sequence of characters. This mix of characters can be lowercase characters, symbols, and numbers. Strings are used to store the user data or sequence of characters through a program.

In some scenarios, converting a string into upper characters can be an essential task. For example, while filling out the online form the programmer is getting information in the string but he or she will store the data in uppercase for sorting purposes.

Uppercase characters are much helpful to improve the readability of the stored data. Also, while sorting the data, uppercase characters are given priority over the mix of lowercase and others. Hence converting strings to upper case is one of the essential tasks in programming.

Why Is There A Need To Convert String Into Uppercase?

Here, we are discussing the real-life need to convert the string data type into uppercase characters. Converting strings to uppercase characters can be an important step while saving data using an application. For more information let’s check some points –

  • Sorting: Uppercase characters are sorted prior. Hence, uppercase characters are given more importance.
  • Importance: Uppercase characters are given more importance than other string methods.
  • Easy to read:  Uppercase characters are easy to read and immediately grasp the attention of the reader. They are identified immediately and get more importance.
  • Storing data: In general, characters are used to store the important data of the user such as a username and other credentials of the person. Important inputs and other details are easily sorted with priority.

These are some points that discuss the real-life need for the conversion over the string data type for the programmer.

Different Approaches To Convert String To Uppercase In Java

We have already discussed that converting strings to practice letters can be an essential task. This convention can be achieved with the help of multiple approaches:

  1. Using the toUpperCase() method
  2. Using the StringJoiner class
  3. Using the StringBuilder class

To learn more about the mentioned approaches and to better understand “how to convert string to uppercase in Java” an explanation of methods, sample code, and their outputs are provided below.

Approach 1 : Convert string to uppercase in Java using the toUpperCase() method

For strings, Java has a built-in method toUpperCase() that can be used to transform all given characters in a string to uppercase characters. This built-in function is a simple and clear means to convert data.

Sample code :

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

         // Define the variable with string characters
        String str = "Geeksfortherescue";

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

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

Output :

GEEKSFORTHERESCUE

Explanation :

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

Approach 2 : Convert to uppercase using the StringJoiner class

StringJoiner class is the utilization class launched by Java. This class is introduced to concatenating the strings.

Sample code :

import java.util.StringJoiner;

public class Main {
    public static void main(String[] args) {
        // Define the variable with string characters
        String str = "hello world";
        
        // Use the Stringjoiner class
        StringJoiner sj = new StringJoiner("");
        char[] charArray = str.toCharArray();
        
        // Use toUpperCase() method for the conversion
        for (char c : charArray) {
            sj.add(Character.toUpperCase(c) + "");
        }
        String uppercaseStr = sj.toString();
        
        // Print the final result
        System.out.println(uppercaseStr);
    }
}

Output :

HELLO WORLD

Explanation :

  1. Import the StringJoiner class
  2. Then define the string
  3. Use the toUpperCase() method for the conversion with loop
  4. Print the output on the console

Approach 3 : Convert to uppercase using the StringBuilder class

StringBuilder class is the Java library that is used to mutable strings. StringBuilder object is created to convert a string into uppercase using a loop. Let’s see the code and explanation.

Sample code :

// Import the libraries
import java.lang.StringBuilder;

public class Main {
    public static void main(String[] args) {
        
        // Define the variable with string characters
        String str = "hello world";
        
        // Use the StringBuilder object 
        StringBuilder sb = new StringBuilder();
        char[] charArray = str.toCharArray();
        
        // Use toUpperCase() method for the conversion
        for (char c : charArray) {
            sb.append(Character.toUpperCase(c));
        }
        String uppercaseStr = sb.toString();
        
         // Print the final result
        System.out.println(uppercaseStr);
    }
}

Output :

HELLO WORLD

Explanation :

  1. Import the StringBuillder class
  2. Then define the string
  3. Use the toUpperCase() method for the conversion with loop
  4. Print the output on the console

Best approach out of three for Converting strings to uppercase in Java

According to our study, the toUpperCase() method is the best of the three techniques for converting string characters to uppercase ones in Java. Let’s understand “why the toUpperCase() method is the best approach“ for the conversion.

Here are several examples that explain our point of view.

  • Predefined built-in function: The String class in Java provides the toUpperCase() method as a built-in method. Because programmers must utilize the technique directly in their code, this way is more convenient. This makes it simpler to use in the application code.
  • Less code: The toUpperCase() function converts string characters to uppercase ones in a straightforward and concise manner. It is simply only one line of code or method, making it simple to use and understandable.
  • Efficiency: ToUpperCase() is a highly optimized and efficient technique. It is supported by advanced algorithms for converting data rapidly and precisely. This increases the productivity of the code making it appropriate for use in high-performance applications.
  • Convenient: More importantly, this approach can change the entire string to uppercase. All other ways can be applied only to one character.

Sample Problems

Sample Problem 1

How to use the toUpperCase() function in Java to convert String to uppercase.  Create a Java program that asks the user for his entire name and then uses the toUpperCase() function to change the text to uppercase. Display the generated uppercase string.

Solution :

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

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: Khemchand
Uppercase string: KHEMCHAND

Sample Problem 2

How to convert from string to uppercase in Java using the StringJoiner class. 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.

Solution :

  1. Import the StringJoiner class
  2. Then ask the student to enter his section
  3. Use the toUpperCase() method for the conversion with loop
  4. Print the output on the console

Code :

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

public class Main {
    public static void main(String[] args) {
        
        // Take the user input
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your section: ");
        String str = input.nextLine();
        
        // Use the Stringjoiner class
        StringJoiner sj = new StringJoiner("");
        char[] charArray = str.toCharArray();
        
        // Use toUpperCase() method for the conversion
        for (char c : charArray) {
            sj.add(Character.toUpperCase(c) + "");
        }
        String uppercaseStr = sj.toString();
        
        // Print the final result
        System.out.println(“Your section is: ” + uppercaseStr);
    }
}

Output :

Enter your section: a
Your section is: A

Sample Problem 3

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

Solution :

  1. Import the StringBuillder class
  2. Ask the user for the initial letter of his name
  3. Use the toUpperCase() method for the conversion with loop
  4. Print the output on the console

Code :

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

import java.lang.StringBuilder;

public class Main {
    public static void main(String[] args) {
        
        // Take the user input
        Scanner input = new Scanner(System.in);
        System.out.print("Enter initial of your name: ");
        String str = input.nextLine();
        
        // Use the StringBuilder object 
        StringBuilder sb = new StringBuilder();
        char[] charArray = str.toCharArray();
        
        // Use toUpperCase() method for the conversion
        for (char c : charArray) {
            sb.append(Character.toUpperCase(c));
        }
        String uppercaseStr = sb.toString();
        
        // Print the final result
        System.out.println("Initial of your name is: " +uppercaseStr);
    }
}

Output :

Enter initial of your name: m
Initial of your name is: M

Conclusion

Uppercase characters are easier to read and eliminate the confusion caused by words that are a string of characters. As a result, programmers are frequently required to transform string characters into uppercase characters. This conversion can be accomplished using one of the three methods indicated above.

Conversion with the toUpperCase() function is the simplest and most helpful way of all. It may be applied to a whole string and requires fewer lines of code.

Other ways are less effective since they can only transform one character at a time. If the programmer has to translate a whole string, he or she can use a loop. However, it consumes additional lines of code and makes the code more complicated.