K. W. H.
K. W. H.

Reputation: 43

Unreachable If Statement

I tried to search for an answer similar to this before posting, so please, go easy on me. I've only been working with Java for about a week and a half. In the following block of code, I'm getting an error, regarding an unreachable if statement on line 20, and an error regarding a missing return statement on 30. I believe that I can just set it to return(0); for the last error, but for the first issue, I can't seem to figure it out.

class Droid {

  int batteryLevel = 100;               //created instance variable, batteryLevel
  int hours;                                //created instance var, hours. val not set yet

  public void activate() {              //created activate method, string type return

    System.out.println("Activated. Hello human. How can I help you?");
    batteryLevel = batteryLevel - 5;  //Set batteryLevel to decrease by 5
    System.out.println("Battery level is " + batteryLevel + " percent.");
 } 

 public int chargeBattery(){        //creat int method chargeBattery


  System.out.println("Droid charging...");
  batteryLevel = batteryLevel + hours;  //setting up recharge for droid
  return(0);                    //return 0 or return nothing?

   if (batteryLevel > 100) {   //If battery level is greater than 100
    batteryLevel = 100;                 //battery level is 100
    System.out.println("Your battery level is at " + batteryLevel + " percent");
    return(0);                  //return 0 or return nothing?
   }    

   else {                               //else, print out battery level
     System.out.println("Your battery level is at " + batteryLevel + " percent");
   }
 }





  public static void main(String [] args) {   // Main method 
  }
}

Upvotes: 0

Views: 402

Answers (5)

Josh
Josh

Reputation: 658

The "problem" is that you're returning before the if statement.

return (0);
if (whatever)
   doSomething;

"return (0)" does 2 things: it immediately ends excution of your current function and "returns" to executing the function yours was called from; and it also passes "0" back to the call of your function. So you are always ending execution of your function before you ever reach the if statement.

I think you want to set your "return value" then continue executing. This is relatively common. Create a new variable called "retVal" or something and set it to zero instead. Then when your function is done executing:

return(retVal);

But you also don't seem to be using your return values very intelligently. You only seem to return "0". And the real "output" of your function is the logging. It seems like you're not actually returning useful data so make your function "void":

public void chargeBattery(){

Since the function isn't expecting to return a value then you don't "need" the return statements at all. When you read the end of the function it'll simply return the execution of code back to the location the function was called from. If you want to end execution "early" then just use:

return;

Upvotes: 2

user3437460
user3437460

Reputation: 17454

Well, this is about the basics of Java methods. In your code:

public int chargeBattery(){        
    System.out.println("Droid charging...");
    batteryLevel = batteryLevel + hours;  
    return(0);           //method will exit from this line onwards
    //Everything from here onwards will be unreachable
}

Be it you are returning a value with return 0 or just return, it will cause the method to exit itself (and continue from where it was being called).

Hence, everything below your return statement will never be reached and Java is 100% sure that it will never be reached because the return statement will always runs first.

However, if your return statement is enclosed within an if-condition, it is a different case because there is still a possibility that the condition may fail hence skipping that return statement:

For example:

public void myMethod(){
    if(whatever)
        return;
    //Code below may still run if above condition fails
}

Upvotes: 1

Mike Irish
Mike Irish

Reputation: 960

as already stated you are returning 0 before your if statement however Do you need to return anything? because you can just change

public int chargeBattery() ->  public void chargeBattery()

then you can remove the return statments.

Upvotes: 0

Kyrre
Kyrre

Reputation: 698

Is it because you return a value before the if statement? The code will set batteryLevel and then return(0), therefore never reaching the if-statement.

Upvotes: 2

Kevin Kennedy
Kevin Kennedy

Reputation: 114

System.out.println("Droid charging..."); batteryLevel = batteryLevel + hours; //setting up recharge for droid return(0); //return 0 or return nothing?

The if statements and all code in unreachable because of the return(0) below batteryLevel = batteryLevel + hours, please remove the return(0) to fix the unreachable error

Upvotes: -1

Related Questions