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.
C# Tip: DO NOT use nameof to value constants
Programming in C# involves making numerous decisions to ensure code efficiency and maintainability. One common aspect is dealing with constants,…
Can we run a Java program without the main method
Can we run a Java program without the main method? The mainly method is executed when we execute a class in…
Java program to calculate students grades in 2 different ways
Java program to calculate students grades: In this post we will learn different ways to calculate the figure from a student…
C# Tip: LINQ’s Enumerable.Range to generate a range of consecutive numbers
When you need to generate a series of numbers in ascending order, you can simply use a while loop with…
3 Ways to Convert System.nanoTime to Seconds in Java
How to convert System.nanoTime to seconds in Java: In this post we will learn how to convert System.nanoTime Unpleasant seconds in…
4 different Java programs to find the HCF or GCD of two numbers
Java programs to find the HCF or GCD of two numbers: Let’s learn how to find the HCF of two numbers…
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