Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This question is part of a lab question, but I want to give the rest a try. This

ID: 3604792 • Letter: T

Question

This question is part of a lab question, but I want to give the rest a try. This is intro to java, so please nothing too advanced.

6.16 Ch 6 Program: Authoring assistant (Java)

(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)

Ex:


(2) Implement a printMenu() method, which outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option. Each option is represented by a single character.

If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call printMenu() in the main() method. Continue to call printMenu() until the user enters q to Quit. (3 pts)

Ex:

I finished number 1, but pasted it for the context. Below is the code I have so far, I need help with the menu.

import java.util.Scanner;
public class AuthoringAssistant {
  
   public static char printMenu(char userMenu){
      Scanner scnr = new Scanner(System.in);
     
      System.out.println("MENU:");
      System.out.println("c - Number of non-whitespace characters");
      System.out.println("w - Number of words");
      System.out.println("f - Find text");
      System.out.println("r - Replace all !'s");
      System.out.println("s - Shorten spaces");
      System.out.println("q - Quit");
      System.out.println("Choose an option: ");
      userMenu=scnr.nextLine().charAt(0);
      if (userMenu=='q'){
         System.out.println("q");
         System.out.println("Quit");
      }
      else if (userMenu=='w'){
         System.out.println("w");
         System.out.println("Number of words: ");
      }
      else if (userMenu=='f'){
         System.out.println("f");
         System.out.println("Find text: ");
      }
      else if (userMenu=='r'){
         System.out.println("r");
         System.out.println("Replace all !'s: ");
      }
      else if (userMenu=='s'){
         System.out.println("s");
         System.out.println("Shorten spaces: ");
      }
      else if (userMenu=='c'){
         System.out.println("c");
         System.out.println("Number of non-whitespace characters: ");
      }
      else {
         System.out.println("Please select a valid character.");
      }
     
      return userMenu;
   }
  
   public static void main(String[] args) {
      String userStr="";
      System.out.println("Enter a sample text:");
      System.out.println("");
     
      Scanner scnr=new Scanner(System.in);
      userStr=scnr.nextLine();
      System.out.println("You entered: " + userStr);
     
      System.out.println("");
      System.out.println(printMenu(userMenu));
     
      return;
   }
}
Below is the error I get.

Your help is much appreciated :) Any code simplification suggestions are much appreciated as well!

Failed to compile AuthoringAssiatant.java:60:ero: cannot find symbol System.out.println (printMenu (userMenu)) 1: variable user symbol Menu location: class AuthoringAssistant 1 error

Explanation / Answer

AuthoringAssistant.java

import java.util.Scanner;

public class AuthoringAssistant {

  

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print("Enter a sample text: ");

String s = scan.nextLine();

System.out.println("You entered: "+s);

int count = 0;

String returnString = "";

while(true){

printMenu();

System.out.println(" Choose an option: ");

char ch = scan.next().charAt(0);

if(ch == 'c' || ch == 'C'){

count = getNumOfNonWSCharacters(s);

System.out.println("Number of non-whitespace characters: "+count);

}

else if(ch == 'w' || ch == 'W'){

count = getNumOfWords(s);

System.out.println("Number of words: "+count);

}

else if(ch == 'f' || ch == 'F'){

System.out.println("Enter a word or phrase to be found: ");

String find = scan.next();

count = findText(s, find);

System.out.println("""+find+"" instances: "+count);

}

else if(ch == 'r' || ch == 'R'){

returnString = replaceExclamation(s);

System.out.println("Edited text: "+returnString);

}else if(ch == 's' || ch == 'S'){

returnString = shortenSpace(s);

System.out.println("Edited text: "+returnString);

}else if(ch == 'q' || ch == 'Q'){

break;

}

}

return;

}

public static void printMenu(){

  

System.out.println("MENU c - Number of non-whitespace characters w - Number of words f - Find text r - Replace all !'s s - Shorten spaces q - Quit");

}

public static int getNumOfNonWSCharacters(String s){

int count = 0;

for(int i=0; i<s.length(); i++){

if(s.charAt(i) != ' '){

count++;

}

}

return count;

}

public static int getNumOfWords(String s){

int count = 0;

if(s.length()>0){

count = 1;

}

for(int i=0; i<s.length(); i++){

if(s.charAt(i) == ' '){

count++;

}

}

return count;

}

public static int findText(String s, String find){

int count = 0;

String words[] = s.split("\s+");

for(int i=0; i<words.length; i++){

if(words[i].equalsIgnoreCase(find)){

count++;

}

}

return count;

}

public static String replaceExclamation(String s){

String returnString = "";

for(int i=0; i<s.length(); i++){

if(s.charAt(i) == '!'){

returnString = returnString +"";

}

else{

returnString = returnString + s.charAt(i);

}

}

return returnString;

}

public static String shortenSpace(String s){

String returnString = "";

boolean isPreviousCharSpace = true;

for(int i=0; i<s.length(); i++){

if(s.charAt(i) != ' '){

isPreviousCharSpace = false;

}

if(isPreviousCharSpace == false){

returnString = returnString + s.charAt(i);

}

if(s.charAt(i) == ' '){

isPreviousCharSpace = true;

}

}

return returnString;

}

}

Output:

Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our jou'
You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our jou'
MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit


Choose an option:
c
Number of non-whitespace characters: 168
MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit


Choose an option:
w
Number of words: 40
MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit


Choose an option:
f
Enter a word or phrase to be found:
dsad
"dsad" instances: 0
MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit


Choose an option:
r
Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our jou'
MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit


Choose an option:
s
Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our jou'
MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit


Choose an option:
q

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote