Ok, so I am working on a homework assignment for java. Here is the assignment: Write a method that finds the number of occurrences of a specified character in a string using the following header: public static int count(String str, char a)
Write a test program that prompts the user to enter a string followed by a character and displays the number of occurrences of the character in the string.
The error in my code is this line: char a = input.nextChar; it says "nextChar cannot be resolved or is not a field"
Here is my code:
import java.util.*;
public class CountOccurances {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
System.out.println("Enter a string: ");
String str = input.nextLine();
System.out.println("Enter a character: ");
char a = input.nextChar;
int letterCheck = count(str, a);
System.out.println("The character " + a + "appeared" + letterCheck + "times in" + str);
}
public static int count(String str, char a)
{
int count = 0;
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == a)
{
count++;
}
}
return count;
}
}