Java - Using a method, how do I "write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Your program must define and call the following method that returns the number of times the input character appears in the input string.
public static int countCharacters(char userChar, String userString)"

So far I have the following:
import java.util.Scanner;

public class LabProgram {

public static int countCharacters(char userChar, String userString)
{
// Create an object of the Scanner class to allow for user inputs
Scanner input = new Scanner(System.in);

// Store text in a variable
userString = input.nextLine();

// Store character in a char variable
userChar = input.next().charAt(0);

// Create and initialize a variable count to zero
// count will hold the number of occurrences of the character in the phrase
int count = 0;

// Loop through each of the character in the string text
// At every cycle of the loop, compare the character of the string
// with the character to be checked.
// If they match, count is incremented
for (int i = 0; i < userString.length(); i++) {
if (userString.charAt(i) == userChar) {
count++;
}

}

// Print out the number of occurrences due to count
System.out.println(userChar + " appears this many times " + count);
}

public static void main(String[] args) {
countCharacters(userChar, userString);
}

}
and my error is that I can't get it to run on main / I don't know where everything goes. I found the code somewhere and I've tried modifying it.

Respuesta :

Answer:Java - Using a method, how do I "write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Y"

Explanation: