This question already has an answer here:
System.out.print("Please input string #1: ");
String string1 = keyboard.nextLine();
System.out.print("Please input a character: ");
char char1 = keyboard.next().charAt(0);
countOccurences(string1, char1);
public static int countOccurences(String s, char c)
{
for(int x = 0; x == s.length(); x++)
if (s.charAt(x) == c)
{
int count;
count++;
}
return count;
The IDE says that the "count" in "count++" is not initialized. Also, it says that the "count" in "int count;" might not have been initialized. "Cannot resolve symbol" for the last count. What am I doing wrong? What I'm trying to do is scan each letter in a string and look for letters that are the same as a certain character. I'm trying to return it but as you can see it doesn't work.
Aucun commentaire:
Enregistrer un commentaire