Answer:
import java.util.Scanner;
public class NumberOfOccurences {
public static void main(String[] args) {
// Instantiate a Scanner object for user Input
Scanner input = new Scanner(System.in);
// Ask user to enter text
System.out.println("Please enter the phrase");
// Save the text in a variable
String phrase = input.nextLine();
// Ask user to input character for checking
System.out.println("Please enter the character to check");
// Capture character in a char variable
char character = input.next().charAt(0);
// Initialize count variable to zero
// Count will store occurrences of the character in the phrase
int count = 0;
// Iterate through each character in the string
// For each loop, compare the character of the string
// with the character being checked.
// If there's a match, increment count
for (int i = 0; i < phrase.length(); i++) {
if (phrase.charAt(i) == character) {
count++;
}
}
// Display the count of occurrences
System.out.println(character + " appears this many times " + count);
}
}
Explanation:
The code has been shared in the response as "NumberOfOccurrences.java". Kindly download the file and review the code along with the comments. Pay close attention to the comments for clarity and comprehension.
Hope this aids you!