Question
Write a Java Program that performs the following: Enter a new main sentence Find a String Find all incidents of a String Find and Replace
Write a Java Program that performs the following:
Enter a new main sentence
Find a String
Find all incidents of a String
Find and Replace a String
Replace all the incidents of a String
Count the number of words
Count a letters occurrences
Count the total number of letters
Delete all the occurrences of a word
Exit
The program will display a menu with each option above, and then ask the user to enter their choice. There should also be a tenth option (option J) to quit, in which case, the program will simply display a goodbye message.
Based on the users choice, the program will perform the chosen functionality - more details are given below for each one. Make sure to include proper output for all cases (such as instructions, questions to the user, or directions telling the user to input something specific). Make sure the menu uses letters not numbers!
Note: The program should keep running and displaying the menu after each functionality, until the user chooses to exit! Make sure to get a default sentence from the user before going into the menu options.
Descriptions of Each Option:
Enter a new main sentence: Ask the user to input a new sentence. This sentence will be later on used in the other menu operations ex: (Find a String, Find all incidents of a String, etc.)
Find a String: Ask the user to input a word, then display the index of the first occurrence of the word in the main sentence. If the word is not found make sure to output the word is not found!
TIP: If you get a runtime error because the word is not found, then your if statement structure is incorrect!
Find all incidents of a String: Ask the user to input a word, then display the index of every occurrence of that word in the main sentence. If the word is not found make sure to output the word is not found!
Find and Replace a String: Prompt the user to input a word, then ask the user to input another new word for replacement. Lastly, locate the word in the main sentence and replace it with the new word. If the user inputs a string thats not found in the main sentence make sure to output the word is not found. Note: This option only replaces the first occurence of the String.
Replace all incidents of a String: Prompt the user to input a word, then ask the user to input another new word for replacement. Lastly, locate the all the occurrences of the word in the main sentence and replace all of them with the new word. If the user inputs a string thats not found in the main sentence make sure to output the word is not found.
Count the number of words: Display the number of words in the main sentence. TIP: Think of what usually separates words and just count the number of occurrences for that.
Count a letters occurrences: Prompt the user to enter a letter. Store that letter in a char variable and then display how many times the char occurs in the main sentence.
Count the total number of letters: Store the main sentence in a temporary variable, try to trim it from spaces. Then use the length method to count how many letters/characters there are in the main sentence.
Delete all the occurrences of a word: Prompt the user to enter a word, then delete all the occurrences of that word in the main sentence then display the updated main sentence.
Note: If the user types at, then all occurrences of at should be removed. Example: station becomes stion. On the other hand, if the user types at with spaces then station is not altered.
Exit: Simply display a goodbye message. End the program.
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