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

James N
James N
17,864 Points

illegal start of type for while loop

I keep getting the following error:

./GoKart.java:27: error: illegal start of type
  while (!isFullyCharged()) {
  ^

can someone please help me?!?!?

4 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

James;

Check out the reply by Stone Preston here. He does a great job of explaining what we are trying to accomplish.

Ken

Can you post your code, especially the function isFullyCharged.

Thanks.

James N
James N
17,864 Points
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 (!isFullyCharged()) {
    mBarsCount++;
  }

}

my error output is:

./GoKart.java:26: error: illegal start of type
  while (!isFullyCharged()) {
  ^
./GoKart.java:26: error: illegal start of type
  while (!isFullyCharged()) {
         ^
./GoKart.java:26: error: ')' expected
  while (!isFullyCharged()) {
          ^
./GoKart.java:26: error: ';' expected
  while (!isFullyCharged()) {
                        ^
./GoKart.java:26: error: illegal start of type
  while (!isFullyCharged()) {
                         ^
./GoKart.java:26: error:  expected
  while (!isFullyCharged()) {
                          ^
./GoKart.java:26: error: ';' expected
  while (!isFullyCharged()) {
                           ^
./GoKart.java:27: error:  expected
    mBarsCount++;
              ^
./GoKart.java:30: error: class, interface, or enum expected
}
^
./GoKart.java:27: error: cannot find symbol
    mBarsCount++;
    ^
  symbol:   class mBarsCount
  location: class GoKart
10 errors

thanks in advance!!

OK. This is in a class declaration. What are you trying to create here? I'll make some assumptions:

An instance of GoKart is driving round a track and it drives over an energy Bar. You then want to increment hEadcOunt unless it has already hit the constant MAX_ENERGY_BARS which is set at eigut for all instances of GoKart.

The while statement is in the wrong place - it needs an event to trigger the Test. It is also recursive (to a point) as it will just repeat until the upper bound is reached. That's probably not what you want to achieve, though.

How about a new method, haveCollectedBar... something like:

Private void haveCollectedBar {

If(!isFullyCharged){
    mBarsCount++
} else {
    // a toast to report a full tank?
}

}

I hope that makes sense.

James N
James N
17,864 Points

thanks! this helped me out A LOT!!!