So I wanna write a program to fulfill these requirements but have problems with using loops, cause the program simply doesn't run. The program should displays a menu for the user to select the following action. This is also displayed again after the action is completed until the user exits the program.
- The menu contains the following items, each of which is to be executed when the named letter is entered:
A) Enter block
B) Determine number of spaces
C) Swap 1st and last word
D) Remove all vowels
X) End
If the user enters a different letter, nothing happens or the menu is displayed again.
If menu item A is selected, a prompt is displayed to enter a block which is read into a string variable. This variable must not be changed by the actions of menu items B, C and D! If necessary a copy of the record must be made before in a further string variable.
If the user executes menu option B, the read in record is run through character by character and the number of blanks in it is counted and then output (e.g. "3 blanks found in record").
Menu item C determines the first and the last word in the sentence (i.e. everything up to the first or everything from the last blank) and then outputs a sentence in which these two parts have been interchanged.
- Example: This turns "Nights are colder than outside" into "Outside is colder than nights".
To determine the position of the 1st space, please use the function indexOf. The position of the last space can then best be determined using lastIndexOf.
Menu item C may only work if the sentence contains at least 2 words and there is no space at the first and last position of the string. Otherwise, the error message 'The sentence must contain at least 2 words and no space must occur at the first and last positions' should appear.
With menu item D, all vowels (whether upper or lower case) are removed from the sentence. The new sentence is then also output.
- Example: From "Hello grandpa, I called Emil" you get "Hll p, ch hb ml ngrfn".
Use a for-loop to run through all vowels, which are stored in a constant beforehand! This way you can search for one vowel at a time and delete it if necessary until all occurrences have disappeared from the string.
The menu items B, C and D may only be selected if a sentence has already been entered before. Otherwise, if B, C or D is entered in the menu, the error message 'No sentence has been entered yet!' is displayed and the menu is displayed again.
Each call of menu items B, C and D always works on the original block entered by the user and not on a block that may have already been changed by previously executed menu items! If, for example, menu item C is executed several times in succession, the result is always the same and the first and last word are not swapped again each time
By entering menu item A again, the entered block can also be overwritten by a new one
With an 'X' the user can terminate the program Translated with www.DeepL.com/Translator (free version) public static void main(String[] args) {
String aktion = null; Scanner scanner = new Scanner(System.in); aktion = scanner.nextLine(); while (!"X".equals(aktion)){ System.out.format("A) Satz eingeben" + "%n" + "B) Anzahl Leerzeichen bestimmen" + "%n" + "C) 1. und letztes Wort vertauschen" + "%n" + "D) Alle Vokale entfernen" + "%n" + "X) Ende" + "%n"); switch (aktion) { case "A": System.out.println("Bitte Satz eingeben:"); str1 = scanner.nextLine(); break; case "B": if (str1 == null) { System.out.println("Noch kein Satz eingegeben!"); } else { System.out.println(SentenceInvestigator.countWhiteSpaces(str1) + " Leerzeichen im Satz gefunden."); } break; case "C": if (str1 == null) { System.out.println("Noch kein Satz eingegeben!"); } else { String str2 = SentenceInvestigator.switchFirstAndLastWord(str1); System.out.println(str2); } break; case "D": if (str1 == null) { System.out.println("Noch kein Satz eingegeben!"); } else { System.out.println(SentenceInvestigator.removeVowels(str1)); } break; case "X": break; } }
}