How do I check if a character representing an alphabet?

In the previous example you’ve learned how to use the Character.isDigit() method to check if a character representing a digit.

In this example you will 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.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.lang;

public class CharacterIsDigitExample {
    public static void main(String[] args) {
        // Manually creates a string with randoms characters.
        String str = "12?XYZ4!6cba78+HI@0";

        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.
X not a digit.
Y not a digit.
Z not a digit.
4 is a digit.
! not a digit.
6 is a digit.
c not a digit.
b not a digit.
a 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.

If there is a specific topic that picked your interest, you can turn to essay helper service (a professional writing service). It is a great alternative to classic googling. The experienced professionals will conduct extensive research on the topic for you and even write an essay if you need one.

How do I know if an ArrayList contains a specified item?

In this example we are going to learn how to find out if an ArrayList object contains the specified element. To check if an ArrayList object contains the specified element we can use the contains(Object o) method. This method returns a boolean true when the specified element is found in the ArrayList, otherwise return false. The method returns true if and only if the list contains at least one element, where the element equals to the item in the list.

Let’s see the code snippet below to demonstrate it.

package org.kodejava.util;

import java.util.ArrayList;
import java.util.List;

public class ArrayListContainsExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");
        list.add("Item 4");

        // Check to see if the list contains "Item 1".
        String itemToFind = "Item 1";
        System.out.println("contains(" + itemToFind + "): " + list.contains(itemToFind));

        // Check to see if the list contains "Item 20".
        itemToFind = "Item 20";
        System.out.println("contains(" + itemToFind + "): " + list.contains(itemToFind));
    }
}

The output of the code snippet above are:

contains(Item 1): true
contains(Item 20): false

How do I know the size of ArrayList?

In this example we are going to learn how to obtain the size of an ArrayList object. As you might already know, a java.util.ArrayList is a class that we can use to create a dynamic sized array. We can add and remove elements from the ArrayList dynamically.

Because its elements can be added or removed at anytime, we might want to know the number of elements currently stored in this list. To obtain the size we can use the size() method. This method returns an int value that tells us the number of elements stored in the ArrayList object.

When the list contains more than Integer.MAX_VALUE elements this method returns Integer.MAX_VALUE.

package org.kodejava.util;

import java.util.ArrayList;
import java.util.List;

public class ArrayListSize {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");

        int size = list.size();
        System.out.println("List size = " + size);
    }
}

In the code snippet above we start creating an instance of ArrayList that can holds String values. As a good practice we should always use the interface as the type of the declared object, in this case we use the List interface. The <> is a diamond operator, started from JDK 7 you can use this operator so that you don’t need to repeat the generic type twice between the declaration and instantiation.

After we create the ArrayList object and add string elements to the list object, we get the size of the list by calling the size() method. We store the result in the size variable and print out its value. If you compile and run the code above you will get the following output:

List size = 3

How do I use ArrayList class?

In this example we will learn how to use the java.util.ArrayList class. An ArrayList is part of the Java Collection Framework. By using this class we can create a dynamic size array of data, which means we can add or remove elements from the array dynamically.

In the code below we will see the demonstration on how to create an instance of ArrayList, add some elements, remove elements and iterate through the entire ArrayList elements either using a for-loop or using the for-each syntax.

We can also see how to convert the ArrayList into an array object using the toArray() method. And we use the Arrays.toString() utility method when we want to print the content of an array.

When creating a class instance it is a good practice to use the interface as the type of variable instead of the concrete type directly. This can make us easily update our code if we don’t want to use ArrayList in the future.

And here is the code snippet:

package org.kodejava.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        // Add items into ArrayList
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");

        // Remove the third item from ArrayList, first index = 0
        list.remove(2);

        // Iterate ArrayList item using for loop statement
        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            System.out.println("Item = " + item);
        }

        // Iterate ArrayList item using enhanced for statement
        for (String item : list) {
            System.out.println("Item = " + item);
        }

        // Iterate ArrayList item using forEach
        list.stream().map(item -> "Item = " + item).forEach(System.out::println);

        // Convert ArrayList into array of object
        String[] array = list.toArray(new String[0]);
        System.out.println("Items = " + Arrays.toString(array));
    }
}

Executing the program will give us the following output printed in our console.

Item = Item 1
Item = Item 2
Item = Item 1
Item = Item 2
Item = Item 1
Item = Item 2
Items = [Item 1, Item 2]