Question: When I tried to compile this example I got 5 error messages. The example instructions for this program is below, and the error messages are

When I tried to compile this example I got 5 error messages.

The example instructions for this program is below, and the error messages are further below.

Ex) Create a PhraseAnalyzer program using isValidPhrase(), getWords(), getSmallestWords(), getLargestWords(), getAverageWordLength(), and getLetterTally() methods. The program should prompt the user to insert a phrase and the program should produce a list including the smallest word used, largest word used, average word length, least frequently used letters, and most frequently used letter. The phrase must also be valid, meaning characters that are not letters or not valid but letters that do not form a word can still count as a word. Ex: asdhj is considered valid.

PhraseAnalyzer.java:90: error: ';' expected in the event that (analyzer.isValidPhrase()) { ^ PhraseAnalyzer.java:90: error: ';' expected in the event that (analyzer.isValidPhrase()) { ^ PhraseAnalyzer.java:90: error: ';' expected in the event that (analyzer.isValidPhrase()) { ^ PhraseAnalyzer.java:101: error: 'else' without 'if' } else { ^ PhraseAnalyzer.java:105: error: expected }I ^ 5 errors

Here is the code below

import java.util.*;

/** * The PhraseAnalyzer class breaks down phrases and gives data about them. */ public class PhraseAnalyzer {

private String phrase;

/** * Builds a PhraseAnalyzer object with the given expression. * * @param phrase the expression to be broke down */ public PhraseAnalyzer(String phrase) { this.phrase = phrase; }

/** * Checks whether the expression is legitimate. * * @return true if the expression is valid, false otherwise */ public boolean isValidPhrase() { return phrase != null && !phrase.isEmpty(); }

/** * Returns an array of words in the expression. * * @return an array of words in the expression */ public String[] getWords() { return phrase.split("\\s+"); }

/** * Returns the smallest words in the expression. * * @return the smallest words in the expression */ public String[] getSmallestWords() { List words = Arrays.asList(getWords()); int smallestLength = Collections.min(words, Comparator.comparing(String::length)).length(); return words.stream().filter(word -> word.length() == smallestLength).toArray(String[]::new); }

/** * Returns the largest words in the expression. * * @return the largest words in the expression */ public String[] getLargestWords() { List words = Arrays.asList(getWords()); int largestLength = Collections.max(words, Comparator.comparing(String::length)).length(); return words.stream().filter(word -> word.length() == largestLength).toArray(String[]::new); }

/** * Returns the average word length in the expression. * * @return the average word length in the expression */ public double getAverageWordLength() { String[] words = getWords(); int totalLength = Arrays.stream(words).mapToInt(String::length).sum(); return (double) totalLength / words.length; }

/** * Returns a map of letter counts in the expression. * * @return a map of letter counts in the expression */ public Map getLetterTally() { Map letterCounts = new HashMap<>(); for (char c : phrase.toCharArray()) { if (Character.isLetter(c)) { letterCounts.put(c, letterCounts.getOrDefault(c, 0) + 1); } } return letterCounts; } public class Principal { public static void main(String[] args) { Scanner = new Scanner(System.in); System.out.print("Enter an expression: "); String phrase = scanner.nextLine(); PhraseAnalyzer analyzer = new PhraseAnalyzer(phrase); in the event that (analyzer.isValidPhrase()) { String[] words = analyzer.getWords(); String[] smallestWords = analyzer.getSmallestWords(); String[] largestWords = analyzer.getLargestWords(); twofold averageWordLength = analyzer.getAverageWordLength(); Map letterTally = analyzer.getLetterTally(); System.out.println("Words: " + Arrays.toString(words)); System.out.println("Smallest words: " + Arrays.toString(smallestWords)); System.out.println("Largest words: " + Arrays.toString(largestWords)); System.out.println("Average word length: " + averageWordLength); System.out.println("Letter count: " + letterTally); } else { System.out.println("Invalid express"); } } }I }

Can someone fix my current example program and then establish what this means? I'm new to coding so help is very much appreciated.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!