How to Check If a Character Is Uppercase in Java

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a character is an uppercase letter in Java. We will explore the Character.isUpperCase() method, a convenient tool provided by the Character class for this purpose.

Through hands-on examples, you will see how to use Character.isUpperCase() with various characters, including uppercase, lowercase, and non-letter characters, to understand its behavior and effectively determine if a given character is uppercase in your Java programs.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("Java")) -.-> java/BasicSyntaxGroup(["Basic Syntax"]) java(("Java")) -.-> java/StringManipulationGroup(["String Manipulation"]) java(("Java")) -.-> java/SystemandDataProcessingGroup(["System and Data Processing"]) java/BasicSyntaxGroup -.-> java/data_types("Data Types") java/BasicSyntaxGroup -.-> java/booleans("Booleans") java/BasicSyntaxGroup -.-> java/if_else("If...Else") java/BasicSyntaxGroup -.-> java/for_loop("For Loop") java/StringManipulationGroup -.-> java/strings("Strings") java/SystemandDataProcessingGroup -.-> java/string_methods("String Methods") subgraph Lab Skills java/data_types -.-> lab-559938{{"How to Check If a Character Is Uppercase in Java"}} java/booleans -.-> lab-559938{{"How to Check If a Character Is Uppercase in Java"}} java/if_else -.-> lab-559938{{"How to Check If a Character Is Uppercase in Java"}} java/for_loop -.-> lab-559938{{"How to Check If a Character Is Uppercase in Java"}} java/strings -.-> lab-559938{{"How to Check If a Character Is Uppercase in Java"}} java/string_methods -.-> lab-559938{{"How to Check If a Character Is Uppercase in Java"}} end

Use Character.isUpperCase() Method

In this step, we will explore how to determine if a character is an uppercase letter in Java using the Character.isUpperCase() method. This method is part of the Character class, which provides useful methods for working with individual characters.

The Character.isUpperCase() method is a static method, which means you can call it directly on the Character class itself, without needing to create an object of the class. It takes a single character as an argument and returns true if the character is an uppercase letter, and false otherwise.

Let's create a simple Java program to demonstrate how to use this method.

  1. Open the HelloJava.java file in the WebIDE editor if it's not already open.

  2. Replace the existing code with the following:

    public class HelloJava {
        public static void main(String[] args) {
            char char1 = 'A';
            char char2 = 'b';
            char char3 = '7';
    
            boolean isUpper1 = Character.isUpperCase(char1);
            boolean isUpper2 = Character.isUpperCase(char2);
            boolean isUpper3 = Character.isUpperCase(char3);
    
            System.out.println("Is '" + char1 + "' uppercase? " + isUpper1);
            System.out.println("Is '" + char2 + "' uppercase? " + isUpper2);
            System.out.println("Is '" + char3 + "' uppercase? " + isUpper3);
        }
    }

    In this code:

    • We declare three char variables: char1, char2, and char3, and assign them different characters.
    • We use Character.isUpperCase() to check if each character is uppercase and store the result in boolean variables (isUpper1, isUpper2, isUpper3).
    • Finally, we print the results to the console.
  3. Save the file (Ctrl+S or Cmd+S).

  4. Compile the program in the Terminal:

    javac HelloJava.java
  5. Run the compiled program:

    java HelloJava

    You should see output similar to this:

    Is 'A' uppercase? true
    Is 'b' uppercase? false
    Is '7' uppercase? false

This output confirms that Character.isUpperCase() correctly identified 'A' as uppercase and 'b' and '7' as not uppercase.

Test with Mixed Case Characters

In the previous step, we used Character.isUpperCase() with individual characters. Now, let's see how we can apply this method to a string containing a mix of uppercase and lowercase characters. This will help us understand how to iterate through a string and check the case of each character.

A string in Java is a sequence of characters. We can access individual characters in a string using the charAt() method, which takes an index (position) as an argument. The index starts from 0 for the first character.

Let's modify our program to check the case of characters in a string.

  1. Open the HelloJava.java file in the WebIDE editor.

  2. Replace the existing code with the following:

    public class HelloJava {
        public static void main(String[] args) {
            String testString = "Hello Java 123";
    
            System.out.println("Checking characters in the string: \"" + testString + "\"");
    
            for (int i = 0; i < testString.length(); i++) {
                char currentChar = testString.charAt(i);
                boolean isUpper = Character.isUpperCase(currentChar);
                System.out.println("Character '" + currentChar + "' at index " + i + " is uppercase? " + isUpper);
            }
        }
    }

    In this updated code:

    • We define a String variable testString with a mix of uppercase letters, lowercase letters, spaces, and numbers.
    • We use a for loop to iterate through each character of the string. The loop runs from index 0 up to (but not including) the length of the string.
    • Inside the loop, testString.charAt(i) gets the character at the current index i.
    • We then use Character.isUpperCase() to check if currentChar is uppercase.
    • Finally, we print the character, its index, and the result of the uppercase check.
  3. Save the file (Ctrl+S or Cmd+S).

  4. Compile the program in the Terminal:

    javac HelloJava.java
  5. Run the compiled program:

    java HelloJava

    You should see output similar to this, showing the result for each character in the string:

    Checking characters in the string: "Hello Java 123"
    Character 'H' at index 0 is uppercase? true
    Character 'e' at index 1 is uppercase? false
    Character 'l' at index 2 is uppercase? false
    Character 'l' at index 3 is uppercase? false
    Character 'o' at index 4 is uppercase? false
    Character ' ' at index 5 is uppercase? false
    Character 'J' at index 6 is uppercase? true
    Character 'a' at index 7 is uppercase? false
    Character 'v' at index 8 is uppercase? false
    Character 'a' at index 9 is uppercase? false
    Character ' ' at index 10 is uppercase? false
    Character '1' at index 11 is uppercase? false
    Character '2' at index 12 is uppercase? false
    Character '3' at index 13 is uppercase? false

This demonstrates how Character.isUpperCase() works when applied to characters extracted from a string, correctly identifying 'H' and 'J' as uppercase.

Ignore Non-Letter Characters

In the previous step, we saw that Character.isUpperCase() returns false for characters that are not letters, such as spaces and numbers. Often, when analyzing text, we are only interested in the letters and want to ignore other characters.

The Character class provides another useful method, Character.isLetter(), which returns true if a character is a letter (either uppercase or lowercase), and false otherwise. We can combine Character.isLetter() and Character.isUpperCase() to check if a character is an uppercase letter and ignore non-letter characters.

Let's modify our program to count the number of uppercase letters in the string, ignoring spaces, numbers, and other non-letter characters.

  1. Open the HelloJava.java file in the WebIDE editor.

  2. Replace the existing code with the following:

    public class HelloJava {
        public static void main(String[] args) {
            String testString = "Hello Java 123";
            int uppercaseCount = 0;
    
            System.out.println("Counting uppercase letters in the string: \"" + testString + "\"");
    
            for (int i = 0; i < testString.length(); i++) {
                char currentChar = testString.charAt(i);
    
                // Check if the character is a letter AND if it is uppercase
                if (Character.isLetter(currentChar) && Character.isUpperCase(currentChar)) {
                    uppercaseCount++;
                    System.out.println("Found uppercase letter: '" + currentChar + "' at index " + i);
                }
            }
    
            System.out.println("Total uppercase letters found: " + uppercaseCount);
        }
    }

    In this code:

    • We initialize an integer variable uppercaseCount to 0.
    • Inside the loop, we add an if condition: if (Character.isLetter(currentChar) && Character.isUpperCase(currentChar)). The && operator means "and". This condition is true only if both Character.isLetter(currentChar) is true and Character.isUpperCase(currentChar) is true.
    • If the condition is true, we increment uppercaseCount and print a message indicating that an uppercase letter was found.
    • After the loop finishes, we print the total count of uppercase letters.
  3. Save the file (Ctrl+S or Cmd+S).

  4. Compile the program in the Terminal:

    javac HelloJava.java
  5. Run the compiled program:

    java HelloJava

    You should see output similar to this:

    Counting uppercase letters in the string: "Hello Java 123"
    Found uppercase letter: 'H' at index 0
    Found uppercase letter: 'J' at index 6
    Total uppercase letters found: 2

This output shows that our program correctly identified and counted only the uppercase letters ('H' and 'J'), ignoring the lowercase letters, spaces, and numbers.

Summary

In this lab, we learned how to check if a character is uppercase in Java using the static method Character.isUpperCase(). This method takes a single character as input and returns a boolean value indicating whether the character is an uppercase letter. We demonstrated its usage with a simple Java program, testing it with uppercase, lowercase, and non-letter characters to observe the returned boolean results.