I am trying to figure out why this one part of my program is not working. I am trying to count the number of the letter s a user types in when prompted using indexOf. For some reason it will not work correctly. Thanks for any help.
import java.util.Scanner;
public class MyStringMethods {
private String myStr="";
public void readString()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter a string: ");
String myStr = keyboard.nextLine();
}
public void setString(String s)
{
myStr = s;
}
public int countOccurrences(String s) // This is the line that will not work with my code.
{
int length = myStr.length();
int countS = 0;
int index = 0;
while (index < length)
{
if (myStr.indexOf(s , 0) == 's' )
{
countS = countS + 1;
}
index = index + 1;
}