Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with getting expected output using input. ONLY case5 and method primeLength/boolean isPrime. If these are able to be put into the same method,

Need help with getting expected output using input. ONLY case5 and method primeLength/boolean isPrime. If these are able to be put into the same method, that is okay as well. My program is crashing because of converting int to boolean.

Input: Kyle floor table Paris hi no my floor

Expected output: Number of Prime in list: 7

CODE BELOW:

================================================================================================

import java.util.Arrays; import java.util.Scanner;

public class WordStatistics { public static void main(String[] args) { getAnswer(); } // end main public static void getAnswer() { Scanner in = new Scanner(System.in); boolean quit = false; int menuItem; System.out.println("Please enter a sentence of words seperated by spaces: "); String sentence = in.nextLine(); String[] words = sentence.split(" ");

System.out.println("1. Display Word List Ordered A-Z"); System.out.println("2. Display Length of each Word in the List"); System.out.println("3. Display Word List Statistics"); System.out.println("4. Count Number of Words with Even/Odd Length"); System.out.println("5. Count Number of Words that have a Prime Length"); System.out.println("6. Enter New Word List"); System.out.println("7. Quit"); // handle user commands do { System.out.print("Choose menu item: "); menuItem = in.nextInt();

switch (menuItem) { case 1: orderedWordList(words); break; case 2: wordLength(sentence); break; case 3: System.out.println("Show method"); break; case 4: evenOddLength(sentence); break; case 5: primeLength(sentence); break; case 6: newWordlist(); break; case 7: quit = true; } } while (!quit); System.out.println("Thank you for using this program, goodbye!"); in.close(); } // end getAnswers public static void orderedWordList(String[] str) { Arrays.sort(str); System.out.println("The sorted array is " + Arrays.toString(str)); } // end orderedWordList method public static void wordLength(String str) { int letterCount = 0; int wordCount = 0; for(int i = 0; i < str.length(); i++) { if(str.charAt(i) == ' ') { System.out.println(str.substring(wordCount, i) + " length is " + letterCount); letterCount = 0; wordCount = i + 1; } else { letterCount++; } // end if } // end for loop } // end orderedWordLength public static void evenOddLength(String str) { int posWordCount = 0; int negWordCount = 0; for (String str1 : str.split(" ")) { if(str1.length() % 2 == 0) { posWordCount++; } else { negWordCount++; } } // end for loop System.out.println("Number even: " + posWordCount); System.out.println("Number odd: " + negWordCount); } // end evenOddLength static boolean isPrime(String str) { // This flag maintains status whether the // x is prime or not int prime = 0; int x; if (str.length() == 2) return 1; if (str.length() > 1) { for (int i = 2; i < Math.sqrt(x) + 1; i++) { if (x % i == 0) { prime = 1; break; } } // end for if (prime == 0) return true; else return false; } //end if else return false; } // end boolean public static void primeLength(String str) { for (String str1 : str.split(" ")) for(int i = 0; i < str.length(); i++) { if(isPrime(str1)) { System.out.println("Number of prime in list: " + str1); } } // end for loop } public static String newWordlist() { Scanner in = new Scanner(System.in); System.out.println("Please enter another sentence of words seperated by spaces: "); String sentence = in.nextLine(); return sentence; } // end newWordList } // end class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Media buying seeking economy, efficiency and effectiveness

Answered: 1 week ago