Java String charAt method explanation with examples

Java String charAt method:

The charAt method of Java string is used to find a character at any given index. This is a predefined method of Java string class and we can use it on any string object in Java.

Let’s learn how this method is defined in Java and how to use it with examples.

Definition of Java String charAt:

The charAt method is defined as below:

public char charAt(int i)
  • This method takes only one parameter. It is i, i.e. the index of the character we want.
  • It returns the character at index i.
  • The index starts from 0 and ends at length of the string - 1.

Exception:

This method might throw IndexOutOfBoundsException if the index i is not valid.

Example to use charAt:

Let’s try this method with an example. The below program takes the index as input from the user and prints the character at that index:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String givenString = "The quick brown fox jumps over the lazy dog";
        int index;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Given String: " + givenString);
        System.out.println("Enter the index of the character: ");
        index = scanner.nextInt();

        System.out.println("Character at this index is: " + givenString.charAt(index));
    }
}

In this example,

  • givenString is the given string.
  • It asks the user to enter the index of the character. It reads that index and assigns that value to the index variable.
  • The last line is printing the character at the user-given index.

It will give output as below:

Given String: The quick brown fox jumps over the lazy dog
Enter the index of the character: 
5
Character at this index is: u

Exception on charAt:

If the user provides any invalid index, it throws an exception. For example, if the input is -1, it will throw an exception:

Given String: The quick brown fox jumps over the lazy dog
Enter the index of the character: 
-1
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
	at java.base/java.lang.String.charAt(String.java:711)
	at com.company.Main.main(Main.java:15)

We can use a try-catch block to handle this exception.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String givenString = "The quick brown fox jumps over the lazy dog";
        int index;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Given String: " + givenString);
        System.out.println("Enter the index of the character: ");
        index = scanner.nextInt();

        try {
            System.out.println("Character at this index is: " + givenString.charAt(index));
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println("Please enter a valid index.");
        }
    }
}

This will print a message on any exception.

You might also like: