Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Sentence and Letter Count Program

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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'.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    // 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;
         }
    }

Similar Threads

  1. [SOLVED] Recursive Sentence Finder
    By raphytaffy in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 21st, 2010, 02:46 PM
  2. making a sentence proper
    By dvsumosize in forum Algorithms & Recursion
    Replies: 5
    Last Post: February 3rd, 2010, 11:01 AM
  3. help with the logic on this letter grade program.
    By etidd in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 09:14 PM
  4. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 07:01 AM
  5. reading a sentence
    By lotus in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: July 20th, 2009, 08:29 AM