How do I iterate each characters of a string?

package org.kodejava.example.text;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

public class StringCharacterIteratorExample {
    private static final String text =
        "The quick brown fox jumps over the lazy dog";

    public static void main(String[] args) {
        CharacterIterator it = new StringCharacterIterator(text);

        int vowels = 0;
        int consonants = 0;

        // Iterates charater sets from the beginning to the last character
        for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                vowels = vowels + 1;
            } else if (ch != ' ') {
                consonants = consonants + 1;
            }
        }

        System.out.println("Number of vowels: " + vowels);
        System.out.println("Number of consonants: " + consonants);
    }
}

How do I create a repeated sequence of character?

This example show you how to create a repeated sequence of characters. To do this we use the Arrays.fill() method. This method fills an array of char with a character.

package org.kodejava.example.util;

import java.util.Arrays;

public class RepeatCharacterExample {
    public static void main(String[] args) {
        char c = 'x';
        int length = 10;

        // creates char array with 10 elements
        char[] chars = new char[length];

        // fill each element of chars array with 'x'
        Arrays.fill(chars, c);

        // print out the repeated 'x'
        System.out.println(String.valueOf(chars));
    }
}

As the result you get the x character repeated 10 times.

xxxxxxxxxx

How do I know if a character is uppercase?

In the previously example How do I know if a character is lowercase?, we’ve learn how to use the Character.isLowerCase() method. Now we will learn how to use the other method, which is the Character.isUpperCase() to determine if a letter is in an uppercase form. This method also return a boolean value that indicates whether the character is in uppercase form or not.

Another way to check if a letter is in uppercase form is by comparing the type of the characters, that can be obtained using the Character.getType() method with a defined constant value Character.UPPERCASE_LETTER.

Below is the code snippet that demonstrate this methods.

package org.kodejava.example.lang;

public class CharacterIsUperCaseExample {
    public static void main(String[] args) {
        char a = 'A';

        // Checks to see if a letter is a uppercase letter.
        if (Character.isUpperCase(a)) {
            System.out.println(a + " is an uppercase character.");
        }

        // Checks to see if a letter is an uppercase letter
        // by comparing the character type against
        // Character.UPPERCASE_LETTER constant value.
        int charType = Character.getType(a);
        if (charType == Character.UPPERCASE_LETTER) {
            System.out.println(a + " is an uppercase character.");
        }
    }
}

The above example give you the following result when it executed:

A is an uppercase character.
A is an uppercase character.

How do I know if a character is lowercase?

In this example you are going to learn another example on using the Character class. The Character.isLowerCase() method can be use to determine if a letter is a lowercase letter. This method takes a char as argument and return a boolean value. If it returns true it means the character is in lowercase form. And if it returns false it means the character is not in lowercase form.

Besides using the isLowerCase() method you can also compare the character type with the constant value of Character.LOWERCASE_LETTER. To get the character type you can use the Character.getType() method. You can see the example on the following code snippet.

package org.kodejava.example.lang;

public class CharacterIsLowerCaseExample {
    public static void main(String[] args) {
        char a = 'a';

        // Checks to see if a letter is a lowercase letter.
        if (Character.isLowerCase(a)) {
            System.out.println(a + " is a lowercase letter.");
        }

        // Checks to see if a letter is a lowercase letter
        // by comparing the character type against
        // Character.LOWERCASE_LETTER constant value.
        int charType = Character.getType(a);
        if (charType == Character.LOWERCASE_LETTER) {
            System.out.println(a + " is a lowercase letter.");
        }
    }
}

This example give you the following output:

a is a lowercase letter.
a is a lowercase letter.

How do I check if a character representing an alphabet?

In the previous example you’ve learn how to use the Character.isDigit() method to check if a character representing a digit. In this example you can learn how to check if a character representing a letter. You can use the Character.isLetter(char c) method to check if a character is a valid letter. This method will return a true value for a valid letter characters and false if the character is not a valid letter.

In the code snippet below you will also learn how to use the toCharArray() method to covert a string into an array of char. Here we use the method so that we can check every character in the for-each loop to see is the character is a letter or not.

package org.kodejava.example.lang;

public class CharacterIsLetterExample {
    public static void main(String[] args) {
        String name = "Kode Java 123";

        // Determines if the specified character is a letter
        if (Character.isLetter(name.charAt(5))) {
            System.out.println("The fifth character (" +
                    name.charAt(5) + ") is an alphabet!");
        }

        // Iterates all characters in the string to see if it is
        // a letter or not.
        for (char c : name.toCharArray()) {
            if (Character.isLetter(c)) {
                System.out.println(c + " is a letter.");
            } else {
                System.out.println(c + " not a letter.");
            }
        }
    }
}

The code will print the following output:

The fifth character (J) is an alphabet!
K is a letter.
o is a letter.
d is a letter.
e is a letter.
  not a letter.
J is a letter.
a is a letter.
v is a letter.
a is a letter.
  not a letter.
1 not a letter.
2 not a letter.
3 not a letter.

How do I check if a character representing a number?

For validation purposes we might need to check if data entered by our users is a valid data for our application to process. A simple mechanism for checking if a character inputted is a digit or not can be done by using the java.lang.Character class isDigit(char c) method.

In this example you will learn how to check if a character representing a number. You will start by creating a string that contains some random alphanumeric characters in it. And then you are going to iterate these characters, get the value at the specific location using the charAt() method. In the last section you are going to check to if the character represent a digit or not.

package org.kodejava.example.lang;

public class CharacterIsDigitExample {
    public static void main(String[] args) {
        // Manually creates a string with randoms characters.
        String str = "[email protected]";

        for (int i = 0; i < str.length(); i++) {
            // Determines if the specified character is a digit
            if (Character.isDigit(str.charAt(i))) {
                System.out.println(str.charAt(i) + " is a digit.");
            } else {
                System.out.println(str.charAt(i) + " not a digit.");
            }
        }
    }
}

These are the outputs of our program:

1 is a digit.
2 is a digit.
? not a digit.
A not a digit.
B not a digit.
C not a digit.
4 is a digit.
! not a digit.
6 is a digit.
d not a digit.
e not a digit.
f not a digit.
7 is a digit.
8 is a digit.
+ not a digit.
H not a digit.
I not a digit.
@ not a digit.
0 is a digit.