hi all, I have the following codes.
This will tell me where the character (char1) is located. However, I am trying to find out how many times char1 has appeared in the sentence. For example: if sentence is "my name is", and char1 is 'm', then it will display 2.
I just can't figure out how to count using indexOf.
Anything helps is appreciated.
public class Occur
public static void main( String args )
int count = 0;
Scanner scanner = new Scanner( System.in );
System.out.println( "Enter a line of text:" );
String sentence = scanner.nextLine();
Scanner input = new Scanner( System.in );
System.out.println( "\nEnter a search character:" );
String char1 = input.nextLine();
System.out.printf( "%s is located at index %d\n", char1, sentence .indexOf( char1 ) );