简体   繁体   中英

Unreachable code using switch statements in Java

So I'm building an email administration app and although it is working with if statements, I am trying to incorporate switch statements. I am prompting the user using a scanner to select which department they are from while building their email associated with it. Although when I am trying to return this.department, it is saying it is unreachable. I feel like I am missing something very obvious.

// Ask for the department
private String setDepartment() {
    Scanner sc = new Scanner(System.in);
    int department;
    
    System.out.println("Select your department:\n1. Sales\n2.Accounting\n3.Development\n4.N/A");
    department = sc.nextInt();
    
    while (true) {
        switch (department) {
            case 1:
                this.department = "Sales";
                break;
            case 2:
                this.department = "Accounting";
                break;
            case 3:
                this.department = "Development";
                break;
            case 4:
                this.department = "";
                break;
            default:
                System.out.println("Please choose a valid option (1-4)");
                break;
        }
        sc.close();
    }
    return this.department; <-- Error: Unreachable code
}

problem is your condition inside the while() loop . It set always to true . So code below the while loop never gets executed .Try to have a different condition inside the while loop which terminates in a certain value.Otherwise you need place what ever you want to return inside the while loop.

Extra: Please also place your sc.close(); outside the while loop otherwise you can't use the resource after the first while loop iteration.so closing resource must be done always at the end.

Solution 1:

while(updatedTerminationCondition){//change the termination condition





}

Solution 2:

while(true){




//place your return value inside here but then loop will only run once  
}

The problem is your switch -statement is in a while(true) loop that gets never exited. Unless you explicitly exit the loop eg by using break the program will never stop to execute the loop, meaning any code below it will never get reached, thats what the compiler is complaining about.

This code may be rewritten to get rid of the endless loop and redundant local variable namesake of the class member:

private String setDepartment() {
    Scanner sc = new Scanner(System.in);
    System.out.println("Select your department:\n1. Sales\n2. Accounting\n3. Development\n4. N/A");
        
    while (null == this.department) {
        switch(sc.nextInt()) {
            case 1:
                this.department = "Sales";
                break;
            case 2:
                this.department = "Accounting";
                break;
            case 3:
                this.department = "Development";
                break;
            case 4:
                this.department = "";
                break;
            default:
                System.out.println("Please choose a valid option (1-4)");
                break;
        }
    }
    return this.department;
}

Update
Also Scanner does not have to be closed if it is using standard input System.in (thanks @rzwitserloot for pointing it out).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM