-2

my while loop in Java after the switch statement crashes and doesnt work for the next step. the user can choose a figure to draw and then some parameters. after its drawn, the menu should come again but all its does after drawn it goes threw the code into the default case from the switch case and i cant write in the console anymore

        Scanner input = new Scanner(System.in);
        Boolean isValid = true;
        while (isValid) {
            System.out.println("Welche Figur möchten Sie eingeben? ");
            System.out.println("(Auswahl: Rechteck, Dreieck oder Kreis)");
            String choosenFigure = input.nextLine();
            switch (choosenFigure.toLowerCase()) {
            case "reechteck":
                itsARectangle(input);
                break;
            case "dreieck":
                itsATriangle(input);
                break;
            case "kreis":
                itsACircle(input);
                break;
            default:
                System.out.println("Keine gueltige Figur!");
                break;
            }
        }

Test Case:

Welche Figur möchten Sie eingeben? 
(Auswahl: Rechteck, Dreieck oder Kreis)
Kreis
Geben Sie den Mittelpunkt an: 
X: 50
Y: 50
Geben Sie die Anzahl der Segmente an: 
10
Geben Sie den Radius an: 
50
Flaecheninhalt: 
22.21441469079183
Welche Figur möchten Sie eingeben? 
(Auswahl: Rechteck, Dreieck oder Kreis)
Keine gueltige Figur! // not what i want
Welche Figur möchten Sie eingeben? 
(Auswahl: Rechteck, Dreieck oder Kreis)
// cant write anymore
CryptoFool
  • 15,463
  • 4
  • 16
  • 36
KevinRump
  • 27
  • 6

1 Answers1

3

What I expect is happening is that inside itsACircle, you are calling nextInt or nextDouble on the input Scanner object. What this does is reads the next numeric value from the console but does not read the newline character that follows the numeric value. So when you exit itsACircle, loop back around, and then call input.nextLine for the second time, the newline character that is still in the input stream from the user entering a number and hitting "Return" causes an empty line to be returned from that call without you typing anything at the terminal.

The solution for this is to call nextLine an extra time after reading numbers, before you call nextLine and use the result. This will remove the extra newline from the input stream so that when you call nextLine again to get a line of input, your program waits for input from the use and proceeds correctly. This is a common problem encountered when using Scanner to read input from the console.

To be clear, here's an example:

String aString = input.nextLine();
int aNumber = input.nextInt();
int anotherNumber = input.nextInt();
input.nextLine();
String anotherString = input.nextLine();

Without the 4th line being there, the 5th line will not stop for user input and will cause anotherString to contain an empty string.

CryptoFool
  • 15,463
  • 4
  • 16
  • 36
  • ok ty very much so i should call input.nextLine() before i break the switch cases ? – KevinRump Dec 17 '20 at 09:04
  • I would add the extra call to each of your shape functions, after you're done asking for numeric input. What you are doing is cleaning up after those calls so that you don't leave unprocessed data in the input stream. – CryptoFool Dec 17 '20 at 16:09