Write a program to take a String as input then display the position of a particular character given as input using indexOf() function of String class. Don't use charAt() function.
Enter a String
JapanIsSupremE
Enter character to find: e
Index Position: 11
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
String line = in.nextLine();
System.out.print("Enter character to find: ");
String toFind = in.nextLine();
System.out.println("Index Position: " + line.indexOf(toFind.toCharArray()[0]));
}
}
Comments
Leave a comment