Prompt the user to enter a string of their choosing. Store the
text in a string. Output the string. (2 pt)
Ex:
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 journeys continue!
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 journeys continue!
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. (4 pts)
Ex:
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:
More coming ANSWER IN JAVA
import java.util.Scanner;
public class StringOperations {
static String text;
public static void printMenu() {
String choose = "y";
Scanner s = new
Scanner(System.in);
while(choose.charAt(0) != 'q')
{
System.out.println("MENU\r\n" +
"\r\n" +
"c - Number of non-whitespace characters\r\n"
+
"\r\n" +
"w - Number of words\r\n" +
"\r\n" +
"f - Find text\r\n" +
"\r\n" +
"r - Replace all !'s\r\n" +
"\r\n" +
"s - Shorten spaces\r\n" +
"\r\n" +
"q - Quit\r\n" +
"\r\n" +
"Choose an option:");
choose =
s.nextLine();
if(choose.charAt(0) == 'c') { //Number of
non-whitespace characters
System.out.println("Number of non-whitespace
characters : " + numOfNonWSChars());
}
else
if(choose.charAt(0) == 'w') { //Number of words
System.out.println("Number of words : " +
numOfWords());
}
else
if(choose.charAt(0) == 'f') { //Find text
System.out.println("Enter text to find :
");
String find = s.nextLine();
System.out.println("Find Text : " +
findText(find));
}
else
if(choose.charAt(0) == 'r') { //Replace all !'s
System.out.println("Replace all !'s : " +
replaceExclamation());
}
else
if(choose.charAt(0) == 's') { //Shorten spaces
System.out.println("Shorten Spaces : " +
shortenSpaces());
}
}
s.close();
}
public static int numOfNonWSChars() {
int count = 0;
String input = text;
for(int i=0; i<input.length();
i++) {
if(input.charAt(i) != ' ') {
count++;
}
}
return count;
}
public static int numOfWords() {
int count = 0;
String input = text;
input =
input.replaceAll("\\p{Punct}", "");
//remove all punctuation marks
input = input.trim();
//remove
leading and trailing whitespaces
input = input.replaceAll(" ", "
"); //replace double space with
single space
String[] list = input.split("
");
count = list.length;
return count;
}
public static String findText(String find) {
String temp = "";
String input = text;
if(input.contains(find)) {
temp = "Text
found at index : " + input.indexOf(find);
}
else {
temp = "Text not
found!";
}
return temp;
}
public static String replaceExclamation(){
String input = text;
input = input.replaceAll("!",
"");
return input;
}
public static String shortenSpaces(){
String input = text;
input = input.trim();
//remove leading and trailing
whitespaces
input = input.replaceAll(" ", "
"); //remove double space
return input;
}
public static void main(String[] args) {
Scanner s = new
Scanner(System.in);
System.out.println("Enter a sample
text:");
String inputText =
s.nextLine();
text = inputText;
System.out.println("You entered: "
+ inputText);
printMenu();
s.close();
}
}
Get Answers For Free
Most questions answered within 1 hours.