This is my code but I don't know how to fix it. the purpose of this code is to ask the user to input a phrase of at least 5 words long and we want to analyze what the longest word is and how many words are in the phrase.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
* Ryan Chin
CS 111B Assignment 4
Count Words and display longest word
*/
import java.util.Scanner;

public class Main
{
publicstaticvoid 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.
(Use a loop to make the user input a new phrase if it is too short.)
@return the phrase
*/

// INSERT YOUR CODE HERE
static String inputPhrase()
{
Scanner scan = new Scanner(System.in);
String phrase;
boolean keepSearching;

do
{
keepSearching = false;
System.out.println("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.");
phrase = scan.nextLine();
if(phrase.length()<5)
{
keepSearching = true;
System.out.println("Your phrase must contain at least 5 characters. ");
}
}while(keepSearching);

return phrase;
}

/**
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.
Return the number of words.
Output the longest word on the screen (console).
@param str string to analyze
@return number of words in str
*/

// INSERT YOUR CODE HERE
staticint analyzePhrase(String phrase)
{
int count =0;
int lastIndex;
String longest = phrase.substring(0, phrase.indexOf(' '));
int ptr1 =0;
int ptr2 =0;
int sizeOfLongest =0;

for(int i=0; i<phrase.length()-1; i++)
{
int size;
if(phrase.charAt(i)==' ')
{
ptr2 = phrase.charAt(i);
count++;
}
size = ptr2 - ptr1;
if(size > sizeOfLongest)
{
longest = phrase.substring(ptr1, ptr2);
ptr1 = ptr2;
}
}
System.out.println("The longest word in your phrase is " + longest + " with " + longest.length() + " characters.");
return count+1;
}
/* Hints:
To count words and determine their lengths, I recommend you use a for-loop to go
through each character one at a time, using .charAt and then when you see a
space ' ' count the word and set a variable to remember which character number
that was, so you can calculate how long each word is. Use a variable for the
longest word seen so far, and compare each word as you see it, to determine if
it is longer than the longest word seen so far.  If so, update that to the
longest word seen so far.  Don't forget about the final word, which doesn't
end with a space.
*/

}

/* Sample Output:

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.


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


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.


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.

*/
 
This is my code but I don't know how to fix it. the purpose of this code is to ask the user to input a phrase of at least 5 words long and we want to analyze what the longest word is and how many words are in the phrase.
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY