String Array to UpperCase Java

String Array To Uppercase Java | Here we will convert the elements of a string array into uppercase by using a built-in method available. After converting the string array to uppercase each string element of the string array will be converted into uppercase. Also see:- String Array to LowerCase Java

Example of string array to uppercase Java:-
String array = {“Java”, “Python”, “JavaScript”}
After converting into uppercase the resultant array would be as follows:-
After conversion = {“JAVA”, “PYTHON”, “JAVASCRIPT”}

The toUpperCase() method of the String class is used to convert String to the upper case. This method is present in java.lang package in string class, as this is the default package there is no need to import explicitly.

There are two variations in the toUppperCase() method:-
1) public String toUppperCase()
2) public String toUpperCase(locale loc)

The toUpperCase() method does not take any parameters and returns the string which is converted into upper case. To know more in detail observe the below examples.

String str = "Welcome to java program";
String upper = str.toUpperCase();
System.out.println(upper);

The resultant string is:-
WELCOME TO JAVA PROGRAM

Convert String Array to UpperCase Java Using toUpperCase()

import java.util.Arrays;
import java.util.Scanner;

public class Main {
   public static void main(String args[]) {
      Scanner scan = new Scanner(System.in);

      System.out.print("Enter the number of array elements: ");
      int n = scan.nextInt();
      scan.nextLine(); // clear

      System.out.println("Enter " + n + " strings: ");
      String array[] = new String[n];
      for (int i = 0; i < n; i++) {
         array[i] = scan.nextLine();
      }
      System.out.println("Entered string array: " 
                        + Arrays.toString(array));

      // convert to uppercase
      for (int i = 0; i < n; i++) {
         array[i] = array[i].toUpperCase();
      }
      System.out.println("String array after converting" 
             +" to uppercase: " + Arrays.toString(array));
      scan.close();
   }
}

Output:-

Enter the number of array elements: 5
Enter 5 strings:
Java
Servlet
Jsp
Spring
Hibernate
Entered string array: [Java, Servlet, Jsp, Spring, Hibernate]
String array after converting to uppercase: [JAVA, SERVLET, JSP, SPRING, HIBERNATE]

Enter the number of array elements: 3
Enter 3 strings:
javascript
react
angular
Entered string array: [javascript, react, angular]
String array after converting to uppercase: [JAVASCRIPT, REACT, ANGULAR]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *