Homework 9: Analyze Phrase

Objective: To write the methods to complete a program, so that it analyzes a phrase and outputs its longest word, plus the number of words

Please complete the program below, without modifying the main method given so that it produces the output shown. String Manipulation is covered in Section 2.5 of the textbook, and in my notes on Calculations.
/* INSERT YOUR NAME HERE
   CS 111A Homework 9 
   Count Words and display longest word
*/

import java.util.Scanner;

public class AnalyzePhrase
{
  public static void main(String[] args)
  {
    String phrase;
    int numWords;

    phrase = inputPhrase();

    numWords = analyzePhrase(phrase);

    System.out.println("Your phrase has " + numWords + " words in it.");
    System.out.println();
  }

  /**
    inputPhrase method
    Ask the user to input a phrase, and make sure it is at least 5 characters long
    @return the phrase
  */

//   INSERT YOUR CODE HERE

  /**
     analyzePhrase method
     Count the number of words in a given string, and determine its longest word.
     Assume the string is well formed and doesn't have leading 
     or trailing spaces, or multiple spaces in a row, and number of words is returned.
     Longest word is output on the console.
     @param str string to analyze
     @return number of words in str
  */

// INSERT YOUR CODE HERE

}

/* Sample Output:

[cpersiko@fog cs111a]$ java AnalyzePhrase
Please enter a phrase all on one line, 
with just one space separating each word, 
and no spaces at the beginning or end of the phrase.
This is my phrase
The longest word in your phrase is "phrase" with 6 characters.
Your phrase has 4 words in it.

[cpersiko@fog cs111a]$ java AnalyzePhrase
Please enter a phrase all on one line, 
with just one space separating each word, 
and no spaces at the beginning or end of the phrase.
Hi
Your phrase must contain at least 5 characters.
Please enter a phrase all on one line, 
with just one space separating each word, 
and no spaces at the beginning or end of the phrase.
Hello
The longest word in your phrase is "Hello" with 5 characters.
Your phrase has 1 words in it.

[cpersiko@fog cs111a]$ java AnalyzePhrase
Please enter a phrase all on one line, 
with just one space separating each word, 
and no spaces at the beginning or end of the phrase.
Now I will try with a very long phrase that contains many words
The longest word in your phrase is "contains" with 8 characters.
Your phrase has 13 words in it.

[cpersiko@fog cs111a]$ java AnalyzePhrase
Please enter a phrase all on one line, 
with just one space separating each word, 
and no spaces at the beginning or end of the phrase.
Programming is cool
The longest word in your phrase is "Programming" with 11 characters.
Your phrase has 3 words in it.
[cpersiko@fog cs111a]$ 

*/

Return to main CS 111A page