-1

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;
    
          }
      }
    

    }

Ethan
  • 1
  • 2
  • 1
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – OH GOD SPIDERS Oct 27 '20 at 11:16
  • Please limit the description to the actual question. You've provided all your requirements but didn't state what your problem is. "simply doesn't run" doesn't say much. – Amongalen Oct 27 '20 at 11:17
  • Note that while this duplicate will fix one error and your loop will get entered, you never change the value of `aktion` inside your loop so once the loop is entered it'll never stop running – OH GOD SPIDERS Oct 27 '20 at 11:17
  • `while (aktion != "X")` should be `while (!"X".equals(aktion))` – Abra Oct 27 '20 at 11:17
  • @OHGODSPIDERS how should I do that? that's why the program just keep running without giving me any output – Ethan Oct 27 '20 at 11:28
  • @Ethan As already said you'll need to add something so that the value changes inside your loop. Whether you ask the user for input again inside the loop or another solution is really up to you. The only thing that matters is that if `aktion` is not equal to `"X"` it needs to become equal to `"X"` at some point in the loop if you want it to end. – OH GOD SPIDERS Oct 27 '20 at 11:33
  • Consider using "equalsIgnoreCase" for user convenience and potentially .trim() the user's leading and trailing whitespace input. This is always a good practice. – supernova Oct 27 '20 at 12:15

1 Answers1

-1

You need to learn how to use a debugger. Then you would discover that the value of variable aktion never changes.

You correctly display the menu for each iteration of the while loop but you only ever ask the user to enter his selection once – and that is before initially entering the loop.

Compare the following code with yours and I also recommend stepping through it with a debugger.

String str1 = null;
String aktion = null;
Scanner scanner = new Scanner(System.in);
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");
    aktion = scanner.nextLine();
    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;
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41