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

What is wrong with my while loop?

Other then the fact that it is empty my syntax says there are problems with what I wrote in the brackets. If someone could point them out that would be very much appreciated.

GoKart.java
public class GoKart {
  public static final int MAX_ENERGY_BARS = 8;
  private String mColor;
  private int mBarsCount;

  public GoKart(String color) {
    mColor = color;
    mBarsCount = 0;
  }

  public String getColor() {
    return mColor;
  }

  public void charge() {
    mBarsCount = MAX_ENERGY_BARS;
  }


  public boolean isBatteryEmpty() {
    return mBarsCount == 0;
  }

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

  } 
} 

3 Answers

Jacob Bergdahl
Jacob Bergdahl
29,118 Points

The while-loop need to be placed inside a method. Right now it is out in the wild, where it cannot be!

Thanks for the help again.

I am still having problem in my syntax saying that an int cannot be dereferenced. If someone could explain that to me that would be great.

Jacob Bergdahl
Jacob Bergdahl
29,118 Points

On what line or what variable?

line 18

Jacob Bergdahl
Jacob Bergdahl
29,118 Points

You've probably altered the code a little, so I'm not sure what line that is, but the probably is most likely that you wrote !mBarsCount.isFullyCharged(), while it should be simply !isFullyCharged(). You're trying to call this method on an integer, which doesn't work and there's also no need to, since you use the variable already in the method. So simply switch the condition in the while() to !sFullyCharged().