IN JAVA PLEASE 7.20 Ch 6 Program: Authoring assistant (Java) (1) Prompt the user
ID: 3914140 • Letter: I
Question
IN JAVA PLEASE
7.20 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 Enter a sample text: ?'ll continue our quest in space. There will be more shuttie flights and more shuttle crews and yes, more volunteers, more civilians, more teachers in space Nothing ends here: our hopes and our journeys continue! You entered: ?'ll continue our quest in space. There will be more shuttle lights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here: our hopes and our journeys continue! (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. Cal printMenu0 in the main0 method. Continue to call printMenu0 until the user enters q to Quit (3 pts) Ex: MENU CNumber of non-whitespace characters W-Number of words f Find text r Replace all ' s -Shorten spaces g-Quit Choose an option:Explanation / Answer
import java.util.Scanner;
public class Strings {
static String text="";
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sample text :");
text=sc.nextLine();
System.out.println("You Entered :"+text);
char menuOption = '0';
do {
// Setting menuOption equal to return value from showMenu();
menuOption = printMenu();
// Switching on the value given from user
switch (menuOption) {
case 'c':
//no of non white space characters
System.out.println(getNumOfNonWSCharacters(text));
break;
case 'w':
//get no of words
System.out.println(getNoOfWords(text));
break;
case 'f':
//find text
FindText(text);
break;
case 'r':
//replace exclamation with .
System.out.println(replaceExclamation(text));
break;
case 's':
//shoten spaces trim the string
System.out.println(shortenSpace(text));
break;
case 'q':
System.out.println("Quitting Program...");
break;
default:
System.out.println("Sorry, please enter valid Option");
}// End of switch statement
} while (menuOption != 'q');
// Exiting message when user decides to quit Program
System.out.println("Thanks for using this Program...");
}
private static String shortenSpace(String text2) {
return text2.trim();
}
private static String replaceExclamation(String text2) {
return text2.replace('!', '.');
}
private static void FindText(String strOrig) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a text to find:");
text=sc.nextLine();
int intIndex = strOrig.indexOf(text);
if(intIndex == - 1) {
System.out.println("not found");
} else {
System.out.println(text+" instances " + intIndex);
}
}
private static int getNoOfWords(String text2) {
return text2.split("\s+").length;
}
private static int getNumOfNonWSCharacters(String string) {
int non_blank_counter = 0;
String myStr = string;
for(int i=0;i<myStr.length();i++)
if ( myStr.charAt( i ) != ' ' )
non_blank_counter++;
return non_blank_counter;
}
public static char printMenu() {
Scanner sc=new Scanner(System.in);
System.out.println("MENU c - Number of non-whiltespace characters w - Number of words f - Find text r - Replace all !'s s - Shorten spaces q - Quit");
System.out.print("Choose an option :");
char c=sc.next().charAt(0);
return c;
}
}