Sentence and Letter Count Program
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.
Code :
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'.
Re: Sentence and Letter Count Program
You don't need a StringTokenizer to figure this out... All you need is to a Scanner object to find words, and then parse through those words to see if they have the letter, and if so how many of that letter there are. Also, you shouldn't hard-code which letter to look for, simply compare it with the letter the user enters.
Code :
// parses a string into word sub-strings
// takes someString, which was the original sentence and only gets one word back at a time. It assumes the user entered char is lower case, but I'm sure you can make sure it is by using bits of the code below.
someString = someString.toLowerCase(); // get rid of all casing
Scanner splitter = new Scanner(someString);
int totalLetterCount = 0;
int wordCount = 0;
while(splitter.hasNext())
{
String oneWord = splitter.next();
int wordLetterCount = 0;
for (int i = 0; i < oneWord.length(); i++)
{
if(oneWord.charAt(i) == userChar)
{
wordLetterCount++;
}
}
if (wordLetterCount > 0)
{
wordCount++;
totalLetterCount += wordLetterCount;
}
}