The Java charAt method is one of the Java String Method which is used to return the Character at specified index position.
In this article we will show you, How to write charAt in Java Programming language with example.
TIP: The index position of the Java chartAt Function will start from 0, Not 1.
Java charAt syntax
The basic syntax of the 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.
- Index_Position: Please specify the index position of a desired character.
Return Value
The Java chartAt Function will return the Character from String_Object at specified Index_Position. If we specify the Index position out of range, then chartAt Function will throw an error.
Java charAt Example
The Java string charAt method is used to return the Character at given index position. In this Java program, We are going to find the same.
JAVA CODE
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); } }
OUTPUT
ANALYSIS
Within this Java charAt function example, First we declared the String variable and assigned a value using following statement
String str = "Learn Java Tutorial";
Following statements will find 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 Java charAt 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 code snippet you can observe that, we are subtracting one from string length. Because, length of a string will count from 1 to n where as index position will start from 0 to n – 1.
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);
Java charAt Example 2
In this string charAt Java program, We are going to ask the user to enter Y or N. Based on the user entered character we will display the message using Java charat.
JAVA CODE
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("\n Please 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"); } } }
OUTPUT
Let us enter different character as the Java chartAt argument
ANALYSIS
The following statement will ask the user to enter Y or N character and next, we are assigning the user entered value to letter variable.
System.out.println("\n Please Enter Y or N letter: "); String letter = sc.next();
Next, we are checking the character at Zero index position using 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 standard way.
Thank You for Visiting Our Blog