Click to See Complete Forum and Search --> : Letter Count Program
monkey0525
March 10th, 2010, 08:52 PM
I'm writing 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.
dlorde
March 11th, 2010, 04:49 AM
Great - keep us informed of your progress, and if you get stuck on something, don't be afraid to ask.
Teachers open the door, but you must enter by yourself...
Chinese proverb
monkey0525
March 11th, 2010, 10:43 PM
import java.util.Scanner;
public class CountSent
{
public static void main(String[]args)
{
Scanner Scan = new Scanner(System.in);
int charCount = 0;
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("Enter a sentence: ");
String userString = Scan.nextLine();
System.out.print("Enter a letter: ");
char userChar = Scan.nextLine().charAt(0);
userString = userString.substring(0, userString.length()-1);
for (int i = 0; i < userString.length(); i++)
{
if (userString.charAt(i) == userChar)
{
charCount++;
}
}
System.out.println( numWords + " words contain the letter "+ userChar +".");
System.out.println( charCount + " " + userChar + " are found in the sentence.");
}
}
Now the output is leaning towards the direction I want it, however it prints:
words contains the letter S.
8 S are found in the sentence.
Rather than the example:
4 words contain the letters S.
8 S are found in the sentence.
It won't print how many words contain the letter entered by the user. What can I do to make it so it matches the example?
postmortem
March 12th, 2010, 01:42 AM
[CODE] tag, please
numWords is not defined
monkey0525
March 12th, 2010, 02:02 AM
import java.util.Scanner;
public class CountSent
{
public static void main(String[]args)
{
Scanner Scan = new Scanner(System.in);
int charCount = 0;
int numWords = 0;
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("Enter a sentence: ");
String userString = Scan.nextLine();
userString = userString.toLowerCase();
System.out.print("Enter a letter: ");
char userChar = Scan.nextLine().charAt(0);
userString = userString.substring(0, userString.length()-1);
for (int i = 0; i < userString.length(); i++)
{
if (userString.charAt(i) == userChar)
{
charCount++;
numWords++;
}
}
System.out.println( numWords + " words contain the letter "+ userChar +".");
System.out.println( charCount + " " + userChar + " are found in the sentence.");
}
}
My apologies.
dlorde
March 12th, 2010, 06:48 AM
The point of the [CODE]...[/CODE] is to keep the formatting so the code is readable. There's no point using them on unformatted code.
If you want to count the number of words that contain a character, you need to check the text word by word. You can use String.split("\\s") to split up the input text into an array of words.
One of the principal objects of theoretical research in any department of knowledge is to find the point of view from which the subject appears in its greatest simplicity...
J. W. Gibbs
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.