Saturday, May 11, 2024
HomeProgrammingJava String charAt method explanation with examples

Java String charAt method explanation with examples

Java String charAt method:

The signAt method of Java string is used to find a character at a 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 signAt method is defined as follows:

public char charAt(int i)
  • This method uses only one parameter. It is ithat is, the index of the desired character.
  • It returns the character at index i.
  • The index starts from 0 and ends with length of string – 1.

Exception:

This method can throw IndexOutOfBoundsException as the index i is not valid.

Example to use charAt:

Let’s try this method with an example. The program below 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 Table of contents variable.
  • The last line prints the character at the user-specified index.

It gives 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 to charAt:

If the user specifies an invalid index, it will throw an exception. For example, if the input -1it 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 one try to 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 for each exception.

RELATED ARTICLES

1 COMMENT

  1. you are in reality a good webmaster The website loading velocity is amazing It sort of feels that youre doing any distinctive trick Also The contents are masterwork you have done a fantastic job in this topic

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

бнанс Створити акаунт on The Interview Process in Clark, Pampanga
Binance注册奖金 on Venus in Scorpio