Question
Hello! I'm having some trouble getting out of this infinite while loop. in JAVA The rest of the methods are correct, basically they input some
Hello! I'm having some trouble getting out of this infinite while loop. in JAVA The rest of the methods are correct, basically they input some text, and then a menu pops up which I included and they choose from the choices to call methods for some info and unless they type 'q' for quit, the program SHOULD only go through the if statement once, then scan for a new choice INSTEAD it loops and calls the same method over, printing out what the method says correctly, albeit infinitely. tried a few different things but each one never checked for the new choice (char) and got out of the loop! help PLEASE type out the code and add in yours to mine :)) I'd rather not change too much of what I already have!
Thank you!
public static void printMenu( System.out.println("MENU "+ c Number of non-whitespace characters " + "W -Number of words "+ f - Find text "+ "r-Replace ! 's " + s -Shorten spaces " + "q-Quit " + "Choose an option:") /MAIN METHOD public static void main(String [1 args //RECEIVES AND PRINTS INPUT Scanner scnr-new Scanner(System.in); System.out.println("Enter a sample text:") String input new String) input - scnr.nextLine); System.out.println("You Entered: "input + "In") //PRINTS MENU AND RECEIVES INPUT printMenu ); String line scnr.next(); while(line.equals("")) line - scnr.nextLine(); while(true) char choice-line.charAt (0) if (choicec) System.out.println("Number of non-whitespace characters "getNumOfNonWSCharacters (input)); printMenu); else if(choice = "wi) System.out.println ("Number of words: " getNumOfWords (input)); //getNumOfWords (0 printMenu (); System.out.println); else if (choicef') System.out.println("Enter a word or phrase to be found:") String checkForscnr.nextLine) System.out.println("\+ checkFor +"\" instances: "+ findText(checkFor, input)) printMenu ); System.out.println(""); else if (choice r') System.out.printlIn("Edited text: replaceExclamation (input)); System.out.println(""); printMenu) else if(choice =- 'f') System.out.println("Enter a word or phrase to be found:"); String checkFor scnr.nextLine(); System.out.println("\" checkFor +"\" instances:"findText (checkFor, input)) printMenu) System.out.println("); else if(choice 'r') System.out.println("Edited text: "replaceExclamation(input)) System.out.println(""); printMenu); else if(choice = System.out.println("Edited text:"shortenSpace (input)); System.out.println(""); 's') printMenu); else if (choice'q') break e lse printMenu); choice scnr.nextLine().charAt (0); scnr.close) public static void printMenu( System.out.println("MENU "+ c Number of non-whitespace characters " + "W -Number of words "+ f - Find text "+ "r-Replace ! 's " + s -Shorten spaces " + "q-Quit " + "Choose an option:") /MAIN METHOD public static void main(String [1 args //RECEIVES AND PRINTS INPUT Scanner scnr-new Scanner(System.in); System.out.println("Enter a sample text:") String input new String) input - scnr.nextLine); System.out.println("You Entered: "input + "In") //PRINTS MENU AND RECEIVES INPUT printMenu ); String line scnr.next(); while(line.equals("")) line - scnr.nextLine(); while(true) char choice-line.charAt (0) if (choicec) System.out.println("Number of non-whitespace characters "getNumOfNonWSCharacters (input)); printMenu); else if(choice = "wi) System.out.println ("Number of words: " getNumOfWords (input)); //getNumOfWords (0 printMenu (); System.out.println); else if (choicef') System.out.println("Enter a word or phrase to be found:") String checkForscnr.nextLine) System.out.println("\+ checkFor +"\" instances: "+ findText(checkFor, input)) printMenu ); System.out.println(""); else if (choice r') System.out.printlIn("Edited text: replaceExclamation (input)); System.out.println(""); printMenu) else if(choice =- 'f') System.out.println("Enter a word or phrase to be found:"); String checkFor scnr.nextLine(); System.out.println("\" checkFor +"\" instances:"findText (checkFor, input)) printMenu) System.out.println("); else if(choice 'r') System.out.println("Edited text: "replaceExclamation(input)) System.out.println(""); printMenu); else if(choice = System.out.println("Edited text:"shortenSpace (input)); System.out.println(""); 's') printMenu); else if (choice'q') break e lse printMenu); choice scnr.nextLine().charAt (0); scnr.close)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started