What is Java charAt method?
The charAt(int) method returns the character in the specified string for a given index number. The index of string starts at zero.
For example, consider this string:
“Hello World Java”
You want to know the character at 6th index position in that string by using charAt method.
Syntax of using charAt Java method
public char charAt(int index)
A few points about the string charAt method:
- The charAt takes an argument which is an int type.
- The method returns the character as char for the given int argument. The int value specifies the index that starts at 0.
- The index range must be between 0 to the length of string -1.
- If the index value is greater than string length or a negative number, an error IndexOutOfBoundsException occurs.
- The charAt method is specified by CharSequence.
The example of using charAt method
In the following example, Java charAt method is used to return the character in a string. I will use the same string as in introductory section. Let us see which characters occur at the index 6 and 12:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class demo_tst { public static void main(String []args) { //How to use charAt method String strCharat = "Hello World Java"; System.out.println("The character at 6 index is: " +strCharat.charAt(6)); System.out.println("The character at 12 index is: " +strCharat.charAt(12)); } } |
The output:
A program to tell if certain characters exist in a string?
By using charAt method, this program will output if a vowel character occurs at the start of the specified string. For that, a string is entered by the user and program will check for these characters. For taking the user input, the Scanner class is used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Scanner; public class demo_tst { public static void main(String []args) { Scanner readInput = new Scanner(System.in); // Reading from System.in System.out.println("Enter a String: "); String str_charAt = readInput.next(); char chr = str_charAt.charAt(0); if (chr == 'a' || chr == 'e' || chr == 'i' || chr == 'o' || chr == 'u') System.out.println("Found the following vowel as first character: " +chr); } } |
See the output of the tried strings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Enter a String: amazing Found the following vowel as first character: a Enter a String: unlucky Found the following vowel as first character: u Enter a String: mango Enter a String: oranges Found the following vowel as first character: o |
What if given index is greater than the length of string?
As mentioned earlier, if you provide a negative number or greater than the string length then an error occurs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class demo_tst { public static void main(String []args) { //How to use charAt method String strCharat = "charAt method"; System.out.println("Index 4 = " +strCharat.charAt(4)); System.out.println("Index 7 = " +strCharat.charAt(7)); System.out.println("Index 15 = " +strCharat.charAt(15)); } } |

You saw, for the index 15, the program generated StringIndexOutOfBoundsException error with the description: String index out of range: 15.
The indexOf method
The indexOf method returns the index number of the given character in the specified string. For example, if we have a string:
“indexOf method in Java”
This is how you may use the indexOf method for getting ‘J’ index number:
x = str.indexOf(‘J’);
The example program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class demo_tst { public static void main(String args[]) { String strIndOf = new String("indexOf method in Java"); int x; x = strIndOf.indexOf('J'); System.out.print("The indexOf 'J' is: " + x ); } } |
The output:
The indexOf ‘J’ is: 18
Learn more about indexOf method with details – Java indexOf method