Unreachable Code Java Compilation Error

2.6k Views Asked by At

I have searched many topics for my problem, read a lot... but they were not exactly as mine.

I'm getting the "Unreachable Code" compilation error from Eclipse IDE.

I don't understand why my code is unreachable! It is simple though! Here it is...

public String doAction(int action, Player P1, Player P2) {
    switch(action) {
        case 'a':
        case 'A':
            CriticalChance =  ThreadLocalRandom.current() .nextInt(CritMin,CritMax+1);
            if(CriticalChance <= 5) {
                int temp = ThreadLocalRandom.current().nextInt(31,50+1);
                P1.setAttack(temp);
            } else {
                P2.deductHp(P1.newNormalAttack());
            }

            if(CriticalChance <= 5) {
                String temp =  String.format("CRITICAL STRIKE! "+P1.getName()+" did "+P1.getAttack()+" damage to "+P2.getName()+"\n");
                return temp;
            } else {
                String temp =  String.format(P1.getName()+" did "+P1.getAttack()+" damage to "+P2.getName()+"\n");
                return temp;
            }

            if(P2.getHp() > 0) {
                String temp =  String.format(P2.getName()+"'s Remaining HP is "+P2.getHp()+"\n");
                return temp;
            } else {
                String temp =  String.format(P2.getName()+" Bled To Death!!\n");
                return temp;
            }
            break;
    }
}

Please Help! If you need more info, plz tell me.

7

There are 7 best solutions below

0
On

It won't ever reach that break statement because right before it are if elses with return statements in each branch, so it'll definitely return from one of those.

0
On

It will never reach the break; statement because you are returning out of the previous if and else.

0
On
    if(CriticalChance <= 5)
    {
        String temp =  String.format("CRITICAL STRIKE! "+P1.getName()+" did "+P1.getAttack()+" damage to "+P2.getName()+"\n");
        return temp;
    }      
    else
    {
        String temp =  String.format(P1.getName()+" did "+P1.getAttack()+" damage to "+P2.getName()+"\n");
        return temp;
    }

below this block, your code is unreachable. Will not executed.

0
On

It is because of your return temp statement as the code after this will never be executed no matter what the value of CriticalChance. The if or else, whichever condition is executed, the function will return and no more code will be executed.

0
On

Since its returning after this:

   if(CriticalChance <= 5)
    {
        String temp =  String.format("CRITICAL STRIKE! "+P1.getName()+" did "+P1.getAttack()+" damage to "+P2.getName()+"\n");
        return temp;
    }       
    else
    {
        String temp =  String.format(P1.getName()+" did "+P1.getAttack()+" damage to "+P2.getName()+"\n");
        return temp;
    }

Any code after this is unreachable(will never be executed) since its returning in both if and else conditions. Hence compiler is complaining.

0
On

next code is unreachable because there is no condition why code should continue after this if-else statement. The code will return a value in either case.

    if(CriticalChance <= 5)
    {
        String temp =  String.format("CRITICAL STRIKE! "+P1.getName()+" did "+P1.getAttack()+" damage to "+P2.getName()+"\n");
        return temp;
    }       
    else
    {
        String temp =  String.format(P1.getName()+" did "+P1.getAttack()+" damage to "+P2.getName()+"\n");
        return temp;
    }

the unreachable:

    if(P2.getHp() > 0)
    {
        String temp =  String.format(P2.getName()+"'s Remaining HP is "+P2.getHp()+"\n");
        return temp;
    }   
    else
    {   
        String temp =  String.format(P2.getName()+" Bled To Death!!\n");
        return temp;
    }

    break;
    }
     }}
0
On

Look at the code starting as if(CriticalChance <= 5). Notice that the "then" and "else" both end with return statements. That means that the statement after the if is unreachable. (And you do the same thing with the next if statement too ...)


I should also point out that you are violating accepted coding standards by naming variables "CriticalChance", "CritMin" and "CritMax". A variable name should always start with a lowercase letter.