Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How would write these in an if else statement inside of a do while loop? I already have done 1 and 6. It is to
How would write these in an if else statement inside of a do while loop? I already have done 1 and 6. It is to be done in java.
In this lab, you will create an environment where the user can cnter a sentance (as a string) and then manipulate that string using 5 basic commands. These manipulations will affect the successive commands that they make- look at the examples for a clear demonstration of this. You will implement these commands using loops and basic string methods. The ONLY String methods that you can use for this lab are: length, concat, charAt, substring, and equ als (or equalsignoreCase When the program begins, it asks the usar to input a String. This input can range from a single lettar to a complete sentence. You can assume that this input will only contain letters (no numbars, spacial characters, ctc.). In other words, there is no need to do input validation on the fi rst input The program will then list the 6 possible commands that the user can choose from (see bedow). The user can use these commands to modify the original sentence. The program will continue to ask for commands until the user enters the "quit" command. The 6 commands are: 1) Reverse: If the user types "reverse with any capitalization, your program will reverse the original string and print the result. Replace First: If the user types "replace first with any capitalization, your program will prompt the user to enter the character to replace and the new character (to replace the old character). Afterwards, it will replace the first instance of the old character with the new character (see example output below)and prints the modified string Replace Last: Ifthe user types "replace last with any capitalization, your program will prompt the user to enter the character to replace and the new character (to replace the old character). 2) 3) Afterwards, it will replace the last instance of the old character with the new character (see example output below) and prints the modified string Remove: If the user types "remove" with any capitalization, your program will prompt the user to enter the character to remove, and which instance it should remove (it can remove any nstance of the character). The program wil then carry out the remove and print the modified string. 4) 5) Remove All: If the user types "remove alr, with any capitalization, your program will prompt the user to enter the character to remove. Then, it removes all instances of the given letter in the sentence. This command also prints the modified sentence to the console. 6) Quit:stops the program. Error Handling The program should print a friendly error message and continue to the next command if any of the following arises: The user tries to replace or ramove a character not in the input string The user tries to replace (or ramove) a digit or a special character (look up Character.is Letter method) The user tries to remove the 3 a from the sentence but there is no 3 There may be additional arror situations that you can catch and handle gracefully. You should If you are unsure if you need to make every attempt to handle these situations when possible. handle a particular error, post a question to Piazza. In all commands above, when the user enters a character, you should assume that case matters. For example, if the user says remove all A'characters,your program should not remove 'a characters. 1) Reverse: If the user types "reverse" with any capitalization, your program will reverse the original string and print the result. Replace First: If the user types "replace first" with any capitalization, your program will prompt the user to enter the character to replace and the new character (to replace the old character). Afterwards, it will replace the first instance of the old character with the new character (see example output below) and prints the modified string Replace Last: If the user types "replace last with any capitalization, your program will prompt the user to enter the character to replace and the new character (to replace the old character). 2) 3) Afterwards, it will replace the last instance of the old character with the new character (see example output below) and prints the modified string ) Remove: If the user types "remove" with any capitalization, your program will prompt the user to enter the character to remove, and which instance it should remove (it can remove any instance of the character). The program will then carry out the remove and print the modified string. 5) Remove All: If the user types "remove all, with any capitalization, your program will prompt the user to enter the character to remove. Then, it removes all instances of the given letter in the sentence. This command also prints the modified sentence to the console. Quit:stops the program. 6) Example 1: Testing Reverse Enter the string to be manipulated Go Dawg Enter your command (reverse, replace first, replace last, remove all, remove) reverse The new sentence is: sgwaD oG Enter your command (reverse, replace first, replace last, remove all, remove reverse The new sentence is: Go Dawgs Enter your command (reverse, replace first, replace last, remove all, remove) Quit ..Exe cution Ends.. Example 2: Testing replace first and last and capitalization of commands Enter the string to be manipulated Co Dawc Enter your command (reverse, replace first, replace last, remove all, remove RePlACE FIRST Enter the character to replace Enter the new character The new sentence is: GoDawcs Enter your command reverse, replace first, replace last, remove all remave replace LAST Enter the character to replace Enter the new character The new sentence is: GoDawg Enter your command reverse, replace first, replace last, remove all remave Quit Execution Ends Example 3: Letter not found with replace first Enter the string to be manipulated co daws Enter your command reverse, replace first, replace last, remove al l remove replace FIRST Enter the character to replace Enter the new character The letter was not found in the word Enter your command reverse, replace first, replace last, remove all remave replace fist Enter the character to replace Enter the new character The new sentence is: Godawcs Enter your command reverse, replace first, replace last, remove all remave Quit Execution Ends Example 4:Teting remove and remove all Enter the string to be manipulated Enter your command reverse, replace first, replace last, remove all remave remove ALL Enter the character to remove The new sentens: cauzz dawgggs Enter your command reverse, replace first, replace last, remove all remave Enter the character to remove Enter the z would you like to renove (Not the index-1 1st, 2 2nd, etc.): The new sentenis: cazu daaggggs Enter your command reverse, replace first, replace last, remove all remave Enter the character to remove Enter the gwould you ke to remove plot the index . 1 The new sentens: cazu daagggs 1st, 2 ; 2nd, etc. Enter your command reverse, replace first, replace last, remove all, remave) remmave Enter the character to remove Enter the g would you like to remove Not the index-1 1st, 2 :)d, e Error: the letter you are trying to remove does not exist Enter your command reverse, replace first, replace last, remove all, remave) Enter the character to remove Enter the a wouldyou ike to remave (Not the index-1-1st, 2- 2nd, etc The new sentence is:czz dawgggs Enter your command reverse, replace first, replace last, remove all, remave remove a Enter the character to remove The new sentence is:cdangsgs Enter your command reverse, replace first, replace last, remove all, remave remove a Enter the character to remove The new sentence is: dangggs Enter your command reverse, replace first, replace last, remove all, remave Quit Execution Ends In this lab, you will create an environment where the user can cnter a sentance (as a string) and then manipulate that string using 5 basic commands. These manipulations will affect the successive commands that they make- look at the examples for a clear demonstration of this. You will implement these commands using loops and basic string methods. The ONLY String methods that you can use for this lab are: length, concat, charAt, substring, and equ als (or equalsignoreCase When the program begins, it asks the usar to input a String. This input can range from a single lettar to a complete sentence. You can assume that this input will only contain letters (no numbars, spacial characters, ctc.). In other words, there is no need to do input validation on the fi rst input The program will then list the 6 possible commands that the user can choose from (see bedow). The user can use these commands to modify the original sentence. The program will continue to ask for commands until the user enters the "quit" command. The 6 commands are: 1) Reverse: If the user types "reverse with any capitalization, your program will reverse the original string and print the result. Replace First: If the user types "replace first with any capitalization, your program will prompt the user to enter the character to replace and the new character (to replace the old character). Afterwards, it will replace the first instance of the old character with the new character (see example output below)and prints the modified string Replace Last: Ifthe user types "replace last with any capitalization, your program will prompt the user to enter the character to replace and the new character (to replace the old character). 2) 3) Afterwards, it will replace the last instance of the old character with the new character (see example output below) and prints the modified string Remove: If the user types "remove" with any capitalization, your program will prompt the user to enter the character to remove, and which instance it should remove (it can remove any nstance of the character). The program wil then carry out the remove and print the modified string. 4) 5) Remove All: If the user types "remove alr, with any capitalization, your program will prompt the user to enter the character to remove. Then, it removes all instances of the given letter in the sentence. This command also prints the modified sentence to the console. 6) Quit:stops the program. Error Handling The program should print a friendly error message and continue to the next command if any of the following arises: The user tries to replace or ramove a character not in the input string The user tries to replace (or ramove) a digit or a special character (look up Character.is Letter method) The user tries to remove the 3 a from the sentence but there is no 3 There may be additional arror situations that you can catch and handle gracefully. You should If you are unsure if you need to make every attempt to handle these situations when possible. handle a particular error, post a question to Piazza. In all commands above, when the user enters a character, you should assume that case matters. For example, if the user says remove all A'characters,your program should not remove 'a characters. 1) Reverse: If the user types "reverse" with any capitalization, your program will reverse the original string and print the result. Replace First: If the user types "replace first" with any capitalization, your program will prompt the user to enter the character to replace and the new character (to replace the old character). Afterwards, it will replace the first instance of the old character with the new character (see example output below) and prints the modified string Replace Last: If the user types "replace last with any capitalization, your program will prompt the user to enter the character to replace and the new character (to replace the old character). 2) 3) Afterwards, it will replace the last instance of the old character with the new character (see example output below) and prints the modified string ) Remove: If the user types "remove" with any capitalization, your program will prompt the user to enter the character to remove, and which instance it should remove (it can remove any instance of the character). The program will then carry out the remove and print the modified string. 5) Remove All: If the user types "remove all, with any capitalization, your program will prompt the user to enter the character to remove. Then, it removes all instances of the given letter in the sentence. This command also prints the modified sentence to the console. Quit:stops the program. 6) Example 1: Testing Reverse Enter the string to be manipulated Go Dawg Enter your command (reverse, replace first, replace last, remove all, remove) reverse The new sentence is: sgwaD oG Enter your command (reverse, replace first, replace last, remove all, remove reverse The new sentence is: Go Dawgs Enter your command (reverse, replace first, replace last, remove all, remove) Quit ..Exe cution Ends.. Example 2: Testing replace first and last and capitalization of commands Enter the string to be manipulated Co Dawc Enter your command (reverse, replace first, replace last, remove all, remove RePlACE FIRST Enter the character to replace Enter the new character The new sentence is: GoDawcs Enter your command reverse, replace first, replace last, remove all remave replace LAST Enter the character to replace Enter the new character The new sentence is: GoDawg Enter your command reverse, replace first, replace last, remove all remave Quit Execution Ends Example 3: Letter not found with replace first Enter the string to be manipulated co daws Enter your command reverse, replace first, replace last, remove al l remove replace FIRST Enter the character to replace Enter the new character The letter was not found in the word Enter your command reverse, replace first, replace last, remove all remave replace fist Enter the character to replace Enter the new character The new sentence is: Godawcs Enter your command reverse, replace first, replace last, remove all remave Quit Execution Ends Example 4:Teting remove and remove all Enter the string to be manipulated Enter your command reverse, replace first, replace last, remove all remave remove ALL Enter the character to remove The new sentens: cauzz dawgggs Enter your command reverse, replace first, replace last, remove all remave Enter the character to remove Enter the z would you like to renove (Not the index-1 1st, 2 2nd, etc.): The new sentenis: cazu daaggggs Enter your command reverse, replace first, replace last, remove all remave Enter the character to remove Enter the gwould you ke to remove plot the index . 1 The new sentens: cazu daagggs 1st, 2 ; 2nd, etc. Enter your command reverse, replace first, replace last, remove all, remave) remmave Enter the character to remove Enter the g would you like to remove Not the index-1 1st, 2 :)d, e Error: the letter you are trying to remove does not exist Enter your command reverse, replace first, replace last, remove all, remave) Enter the character to remove Enter the a wouldyou ike to remave (Not the index-1-1st, 2- 2nd, etc The new sentence is:czz dawgggs Enter your command reverse, replace first, replace last, remove all, remave remove a Enter the character to remove The new sentence is:cdangsgs Enter your command reverse, replace first, replace last, remove all, remave remove a Enter the character to remove The new sentence is: dangggs Enter your command reverse, replace first, replace last, remove all, remave Quit Execution EndsStep 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