Java charAt Method

The Java charAt method is one of the Java String Methods, which is to return the character at the specified index position. In this article, we will show how to write string charAt in Java Programming language with an example.

Java charAt syntax

The basic syntax of the string charAt in Java Programming language is as shown below.

public char chatAt(int Index_Position) 

//In order to use in program
String_Object.charAt(int Index_Position)
  • String_Object: Please specify the valid String Object to find the character.
  • Index_Position: Please specify the index position of the desired character.

The Java charAt Function will return the Character from String_Object at specified Index_Position. If we specify the Index position out of range, then the chartAt Function will throw an error.

TIP: The position of an index for Java charAt string Function will start from 0, Not 1.

Java charAt Example

In this Java program, We are going to use the Java string charAt method to return the Character at the given index position.

package StringFunctions;

public class CharAtMethod {

 public static void main(String[] args) {
 String str = "Learn Java Tutorial";
 
 char str1 = str.charAt(0);
 char str2 = str.charAt(6);
 char str3 = str.charAt(11);
 char str4 = str.charAt(str.length()- 1);
 
 System.out.println( "Charcter at Index position 0 = " + str1);
 System.out.println( "Charcter at Index position 6 = " + str2);
 System.out.println( "Charcter at Index position 11 = " + str3);
 System.out.println( "Charcter at Last Index position = " + str4);
 }
}
Java charAt Method 1

Within this Java charAt function example, we declared the String variable and assigned a value using the following statement

String str = "Learn Java Tutorial";

The following statements will use the charAt and finds Characters at index positions 0, 6, 11, and store the characters in str1, str2, and str3 variables of type char.

char str1 = str.charAt(0);
char str2 = str.charAt(6);
char str3 = str.charAt(11);

NOTE: For the charAt String Method, you should count the space as One Character.

In the next line, we are using the length function to find the string length.

char str4 = str.charAt(str.length()- 1);

From the above Java code snippet, we are subtracting one from string length. Because, length of a string will count from 1 to n where the index position will start from 0 to n – 1.

The following System.out.println statements will print the output

System.out.println( "Charcter at Index position 0 = " + str1);
System.out.println( "Charcter at Index position 6 = " + str2);
System.out.println( "Charcter at Index position 11 = " + str3);
System.out.println( "Charcter at Last Index position = " + str4);

String charAt Example 2

In this string charAt program, We are going to ask the user to enter Y or N. Based on the user entered character, the Java charAt program will display the message.

package StringFunctions;

import java.util.Scanner;

public class CharAtMethod2 {
 private static Scanner sc;

 public static void main(String[] args) {
 sc = new Scanner(System.in);
 
 System.out.println("\nPlease Enter Y or N letter: ");
 String letter = sc.next();
 char ch = letter.charAt(0);
 
 if (ch == 'y' || ch == 'Y') {
 System.out.println("Hey!! Welcome to String Library");
 }
 else {
 System.out.println("Goodbye to String Library");
 }
 }
}
Please Enter Y or N letter: 
y
Hey!! Welcome to String Library

Let us enter different character as the Java chartAt argument

Please Enter Y or N letter: 
N
Goodbye to String Library

The following statement will ask the user to enter Y or N character. Next, we are assigning the user entered value to the letter variable.

System.out.println("\n Please Enter Y or N letter: ");
String letter = sc.next();

Here, we are checking the character at the Zero index position using the java string charAt method.

char ch = letter.charAt(0);

Next, we used the If Else Statement to check where the character at index position zero is y / Y or not.

  • If it y or Y System.out.println(“Hey!! Welcome to String Library”); statement will be printed
  • Else System.out.println(“Goodbye to String Library”); statement will be printed.

NOTE: we can use the lowercase method to convert user input to lower also, but to make the code easy, we are going in the standard way.