Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Harnessing the Power of Objects Incrementing

Anne Lee
Anne Lee
4,121 Points

I'm getting the error: missing return statement

public boolean isFullyCharged() { while (!isFullyCharged()) return mBarsCount == MAX_ENERGY_BARS; } This is the code I have with a return statement, not sure why I'm getting an error.

r h
r h
68,552 Points

try

public boolean isFullyCharged() { 
        return mBarsCount == MAX_ENERGY_BARS; 
}

The while loop would go in your actual program, not the isFullyCharged() function. What's happening at the moment is that your while loop is running in infinity, because it is recursively calling itself (!isFullyCharged()) in the method itself. You don't want to use recursion for a task like this.

2 Answers

It should return a boolean T/F since its a boolean function. try using if statement too and return it either as true or false!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Anne,

you need to modify the charge() method to make it more flexible and be able to increment mBarsCount if the battery is not fully charged yet.

So inside charge() {} you can use a while loop, that is analizing the return statement of the isFullyCharged method. So if it not true (the mBarsCount is not MAX_ENERGY_BARS) the loop will increment the mBarsCount by 1.

 public void charge() {
    while(!isFullyCharged()) {
      mBarsCount++;
    }
      //mBarsCount = MAX_ENERGY_BARS;
      // >>>>> we can scip this statement, because we created the loop to increment the mBarsCount
      // >>>>> until the return statement of the isFullyCharged method is not true
  }

Grigorij