Check If String is Uppercase Java

Check If String is Uppercase Java | Java provides functionality to check whether the given string is in uppercase or not. In Java Character class contains isUpperCase() method which checks whether the given character is in uppercase or not. And the String class contains the toUpperCase() method to convert the string into uppercase.

With the help of one of these methods, we can check if the string is uppercase in Java. Now let’s see how to check if a string is uppercase in Java?

Check If String is Uppercase Java using toUpperCase() & equals()

We can check if the string is uppercase in Java by using the equals() method with toUpperCase() without converting string elements into characters. For this first, we will convert the given string into uppercase and compare it with the original string. If both strings are the same then the original string is in uppercase. 

The below program returns “Uppercase” if the string is in uppercase else returns “Not Uppercase”.

Java Program to Check If String is Uppercase

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter String: ");
      String str = scan.nextLine();
      if (str.equals(str.toUpperCase())) {
         System.out.println("Uppercase");
      } else {
         System.out.println("Not Uppercase");
      }
      scan.close();
   }
}

Output:

Enter String: KnowProgram
Not Uppercase

Enter String: KNOWPROGRAM
Uppercase

Enter String: KNOW PROGRAM
Uppercase

Check If String is Uppercase Java using Character.isUpperCase()

Now we will see how to check if the string is uppercase in Java by using isUpperCase() method. To check if the string is uppercase or not, instead of checking the whole string at a time we can also check each & every character of the string.

There is a built-in method in the Java Character class called isUpperCase() method which checks for each character whether it is in uppercase or not. This class is present in java.lang package.

Method Syntax:- public boolean character.isUpperCase(char ch)

  • Parameter:-  Character, it takes a character array as a parameter.
  • Returns:- Returns a boolean value as output, that is returns true or false.

The character.isUpperCase() method returns a boolean value that is if the given string is in uppercase then it returns true else it returns false.

Java Check If String is Uppercase using isUpperCase()

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter String: ");
      String str = scan.nextLine();
      System.out.println(isStringUpperCase(str));
      scan.close();
   }

   private static boolean isStringUpperCase(String str) {
      char[] charArray = str.toCharArray();
      for (int i = 0; i < charArray.length; i++) {
         if (!Character.isUpperCase(charArray[i]))
            return false;
      }
      return true;
   }

}

Output:-

Enter String: KnowProgram
false

Enter String: KNOWPROGRAM
true

Enter String: KNOW PROGRAM
false

In the above code, we have defined a static function called isStringUpperCase() which returns a boolean value. In this function, we converted the string to the array of characters by using toCharArray() because isUpperCase() method of the Character class takes the only character.

Then we use the built-in Character.isUpperCase() method to check for the string then return true or false accordingly. In the main method, we called the isStringUpperCase() function and print the output.

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.