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 trialCharles Rotenberg
736 PointsWhat 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.
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
29,119 PointsThe while-loop need to be placed inside a method. Right now it is out in the wild, where it cannot be!
Charles Rotenberg
736 PointsI 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
29,119 PointsOn what line or what variable?
Charles Rotenberg
736 Pointsline 18
Jacob Bergdahl
29,119 PointsYou'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().
Charles Rotenberg
736 PointsCharles Rotenberg
736 PointsThanks for the help again.