Find The Character At A Particular Index Example Java Program

Definition

This method charAt() returns character at a particular index in the string.

Syntax

charAt(string,integer)

Find The Character At A Particular Index Example Program

import java.util.Scanner;

class FindCharacterAtIndex{
	public static void main(String[] args){
		String str="abcdefghijklmnopqrstuvwxyz";
		System.out.println("The string is: "+str);
		Scanner in=new Scanner(System.in);
		System.out.print("Enter the index at which the character is to be found: ");
		int index=in.nextInt();
		if(index<=26){
			System.out.println("The character at the index "+index+" is: "+str.charAt(index));
		}
		else{
			System.out.println("Enter a number between 0 and 25");
		}
	}
}

Sample Output

Output is:
The string is: abcdefghijklmnopqrstuvwxyz
Enter the index at which the character is to be found: 8
The character at the index 8 is: i