Question

Prompt the user to enter a string of their choosing. Store the text in a string....

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

Homework Answers

Answer #1

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();      

   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is C. Please write it C. 1) Prompt the user to enter a string of...
This is C. Please write it C. 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: 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...
JavaScript When the button is pressed, the application should prompt the user to enter a string...
JavaScript When the button is pressed, the application should prompt the user to enter a string or ‘***’ to quit, and then remove all instances of the following substring “erd” in the input string. Assume no spaces in the string. Make the entire string lowercase to start with. It should show the parsed string (with the words already removed) in the text area. The application should then again do the above, clearing out the text area before each iteration of...
JavaScript When the button is pressed, the application should prompt the user to enter a string...
JavaScript When the button is pressed, the application should prompt the user to enter a string or ‘***’ to quit, and then print output in the text area specifying how many vowels were in the string. Vowels are the letters: a,e,i,o,u. The application should then again do the above, until the user enters ‘***’. It should then alert ‘Thanks for using the vowel counter’ and exit.
15.20 Ch 9 Warm up: Parsing strings (C++) (1) Prompt the user for a string that...
15.20 Ch 9 Warm up: Parsing strings (C++) (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
This program will store roster and rating information for a soccer team. Coaches rate players during...
This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int vector and the ratings in another int vector. Output these vectors (i.e., output the roster). (3 pts) Ex: Enter player 1's jersey number: 84 Enter player 1's...
5.27 LAB*: Program: Soccer team roster (Vectors) This program will store roster and rating information for...
5.27 LAB*: Program: Soccer team roster (Vectors) This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int vector and the ratings in another int vector. Output these vectors (i.e., output the roster). (3 pts) Ex: Enter player...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). Extend...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). Extend the ItemToPurchase class per the following specifications              Private fields string itemDescription - Initialized in default constructor to "none" Parameterized constructor to assign item name, item description, item price, and itemquantity (default values of 0).             Public instance member methods setDescription() mutator & getDescription() accessor (2 pts) printItemCost() - Outputs the item name followed by the quantity, price, and subtotal printItemDescription() - Outputs the...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
11.12 LAB*: Program: Online shopping cart (continued) This program extends the earlier "Online shopping cart" program....
11.12 LAB*: Program: Online shopping cart (continued) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with...