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 trialMatthew Earlywine
2,560 PointsWhile loop
I thought I found the answer to this question, but I still got an error message.
Okay, so let's use our new isFullyCharged helper method to change our implementation details of the charge method. Let's make it so it will only charge until the battery reports being fully charged. Let's use the ! symbol and a while loop. Inside the loop increment mBarsCount.
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() {
while (! isFullyCharge()); {
mBarsCount ++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_ENERGY_BARS;
}
}
3 Answers
Dennis Mårtensson
7,400 PointsAh. There's a typo. It should be "while (! isFullyCharged())". Forgot the "d" at the end of "isFullyCharged()", and you also had a semicolon that shouldn't be there!
Matthew Earlywine
2,560 PointsI changed it to while (! isFullyCharged()) but I still got an error message
Bummer! Use ++ to increment the mBarsCount member field.
Jonathan Walker
1,984 Pointspublic void charge() { while (! isFullyCharge()); { mBarsCount ++;
Should be "isFullyCharged"
Also, should not be a space between "!" and "isFullyCharged".
Dennis Mårtensson
7,400 PointsActually, the space between "!" and "isFullyCharged()" is legal, the compiler ignores whitespace. Although it looks a tad bit off, it will still compile!
Matthew Earlywine
2,560 PointsThanks for all your help! I didn't think a space between ! Fullycharged and mBarsCount ++ mattered. Why does the space matter though?
Alex Brown
3,555 PointsAlex Brown
3,555 PointsWhat error message was it? That would help with finding the solution.