I'm supposed to write a program which prompts the user to enter a sentence and then prompt the user to enter a single letter of the alphabet (into a char value). It's also supposed to count and display the number of words containing the letter entered (not case sensitive) and the total number of occurrences of the letter entered (not case sensitive). An example if the user entered the sentence: 'She sells seashells by the seashore.' and the letter 'S', the output from the program would be:
4 words contain the letters S.
8 S are found in the sentence.
import java.util.StringTokenizer; import java.util.Scanner; public class CountSent { public static void main(String[]args) { Scanner scan = new Scanner(System.in); String sentInput = null, oneWord = null; StringTokenizer st = null; String letter = ""; System.out.println("\n\nThis program asks you to type in a sentence,"); System.out.println("it then requires a single letter of the alphabet"); System.out.println("to be entered in and displays the number of words"); System.out.println("as well as the total number of occurrences."); System.out.print("\n\nEnter a sentence: "); sentInput = scan.nextLine(); System.out.print("\n\nEnter a letter: "); numInput = scan.nextLine(); sentInput = sentInput.substring(0, sentInput.length()-1); st = new StringTokenizer(sentInput); oneWord = st.nextToken(); letter = oneWord; while (st.countTokens() > 0) { oneWord = st.nextToken(); if (oneWord.length() >= letter.length()) letter = oneWord; for (int index = 0; index < oneWord.length(); index++) if (oneWord.charAt(index) == 'G' || oneWord.charAt(index) == 'g') { System.out.println(oneWord); index = oneWord.length (); } } System.out.println("\n\n" + letter + "letters contain the letter 'G'."); System.out.println( + letter.length() + " are found in the sentence."); } }
The above is how far I've gotten on the coding. The obvious problem is, I have no clue how to contain the letter entered by the user and have it displayed the number of times it was printed in the sentence. The only, for lack of a better word, lead I have is the tidbit with the letter 'G'.