package de.extrema.java;
import java.util.Scanner;
public class Pogramm {

    public static void main(String arrg[]) {
         try (Scanner scanner = new Scanner(System.in)) {
            double a , b , r , A , h; 
            int number2;
            int number;
            System.out.println("Körper: ");
            System.out.println("Deine Wahl: ");
            number = scanner.nextInt();
            number2 = scanner.nextInt();

            switch(number) {
            case 1:{    
                switch(number) {
                System.out.println("Viereck: 1  Kreis: 2  Dreieck: 3  Trapez: 4 ");
                case 1:{
                    System.out.println("a: ");
                    a = scanner.nextDouble();
                    System.out.println("b(a): ");
                    b = scanner.nextDouble();
                    A = a * b;
                    System.out.println("Fläche des Vierecks= " + A);
                    break;

                    }

                case 2:{
                    System.out.println("r: ");
                    r = scanner.nextDouble();
                    A = Math.PI * r * r;
                    System.out.println("Fläche des Kreises= " + A);
                    break;
                    }

                case 3:{
                    System.out.println("a: ");
                    a = scanner.nextDouble();
                    System.out.println("b: ");
                    b = scanner.nextDouble();
                    A = 0.5 * a * b;
                    System.out.println("Fläche des Dreiecks: " + A);
                    break;
                    }
                case 4:{
                    System.out.println("a: ");
                    a = scanner.nextDouble();
                    System.out.println("b: ");
                    b = scanner.nextDouble();
                    System.out.println("h: ");
                    h = scanner.nextDouble();
                    A = (a + b)* h / 2;
                    System.out.println("Fläche des Trapezes: " +A);
                    break;  
                    }
                }
                }

                                 }
            }

            }

        }

Dani AI

Generated

Short diagnosis: the compiler is flagging the outer switch because its body starts with a statement instead of a case/default label. In Java a switch(expr) { ... } must contain case <const>: or default: labels immediately after the opening brace; any free statement (for example a System.out.println(...)) placed before the first case makes the switch malformed and produces the error reported on the switch line. That’s why the error appears at line 17 even though the offending statement is the next line.

Recommended concrete fixes (and why):

  • Put the user prompt outside the switch, or place it inside the appropriate case. Don’t leave a println between switch(...) { and the first case:.
  • If you meant the inner menu to depend on the second input, switch on the second value (the one you read into number2), not the same number twice.
  • Always include a default case so invalid input is handled, and keep each case terminated with break (or use explicit returns / method calls).

Minimal structural example:

switch (bodyChoice) {
case 1:
    System.out.println("Viereck:1 Kreis:2 Dreieck:3 Trapez:4");
    switch (shapeChoice) {
    case 1:
        // rectangle area...
        break;
    case 2:
        // circle area...
        break;
    default:
        System.out.println("Invalid shape");
    }
    break;
default:
    System.out.println("Invalid body choice");
}

Practical debugging tips: compile often while changing small parts; comment out the inner switch to confirm the outer switch compiles; check for missing colons after case labels, mismatched braces, and accidental semicolons after case lines. Note: the parameter name arrg is allowed — the JVM cares about the parameter type, not the identifier, so renaming it is only cosmetic. For clarity and maintenance, consider extracting each area calculation to its own method. This will make the control flow and compile errors much easier to spot.

Recommended Answers

All 5 Replies

You can only get to line 17 if number is 1, so having a switch on number at line 17 is pointless. Did you mean number2 ?
The switch on line 15 has only one case - what's the point of that?
Line 18 is invalid - all the code inside a switch has to be in case blocks

I think Drax from Guardians Of The Galaxy would say:

Why is line 17?

That is, it seems odd to place a switch() statement there.

Line 5 has typo: arrg should be arg.
Why have 1 case on line 17? Just remove that.
You only need one case statement below it.
It is like this was entered in one hit instead of debugging as you went. This is not a good way to program - big bang.

identical switch within switch is just wrong, and printing inside swtch ouside any case is not at all nice.

The error is really on line 18, but it is reported on line 17. The switch statement must be followed by case clauses, each with a constant value for comparison. Line 18 is not a case clause, so the compiler reports that the switch statement is malformed. Also, individual cases clauses do not need to be enclosed in { curlly braces }.

The logic of putting one switch statement inside another, comparing the same value, is unclear to me; you should examine exactly what you intended to do here. Because the inner switch statement is inside a clause where the value is already determined to be 1, the first case will only ever be executed and everything else will be skipped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.