1

New to java. Practicing coding by following a book.

Heres my code:

class Motorcycle {


    //Three instance variables - make and color are strings. while a boolean refers to TRUE OR FLASE(in this case off or on)
    String make;
    String color;
    boolean engineState;

    void startEngine() {
        if (engineState == true)
            System.out.print("The engine is already on.");
        else {
            engineState = true;
            System.out.print("The engine is now on.");

        }

    void showAtts() {
        System.out.print("This motorcycle is a " + color + " " + make);
        if (engineState ==true)
            System.out.print("The engine is on.");
        else System.out.print("The engine is off.");

    }
}
}

When I compile I get 2 errors:

1) illegal start of expression 2) ; expected

I can't pin point the problem. If anyone can direct me or hint me please do.

2
  • 1
    Use of an IDE such as NetBeans, Intellij or Eclipse can really help avoid errors like this. I think even some basic editors like Notepad++ and JEdit have some language support (in terms of syntax highlighting and bracket matching) Commented Jun 27, 2013 at 4:24
  • Sounds like great advice. Downloading now. Commented Jun 27, 2013 at 4:26

7 Answers 7

3

One of your braces was in the wrong place. Should be:

class Motorcycle {

//Three instance variables - make and color are strings. while a boolean refers to TRUE OR FLASE(in this case off or on)
  String make;
  String color;
  boolean engineState;

  void startEngine() {
    if (engineState == true)
      System.out.print("The engine is already on.");
    else {
      engineState = true;
      System.out.print("The engine is now on.");
    }
  }
  void showAtts() {
    System.out.print("This motorcycle is a " + color + " " + make);
    if (engineState ==true)
      System.out.print("The engine is on.");
    else System.out.print("The engine is off.");
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

the method startEngine doesn't have its closing curly brace, and there's another spare closing brace at the end of the code

Comments

2

You have defined showAtts() method inside startEngine() method. A method cannot have definition of another method.

This might be due to braces being placed wrongly. Correct them.

Comments

2
class Motorcycle {

    // Three instance variables - make and color are strings. while a
    // boolean refers to TRUE OR FLASE(in this case off or on)
    String make;
    String color;
    boolean engineState;

    void startEngine() {
        if (engineState == true)
            System.out.print("The engine is already on.");
        else {
            engineState = true;
            System.out.print("The engine is now on.");

        }
    }

    void showAtts() {
        System.out.print("This motorcycle is a " + color + " " + make);
        if (engineState == true)
            System.out.print("The engine is on.");
        else
            System.out.print("The engine is off.");

    }
}

Comments

2

You are trying to define a method inside another method:

void startEngine() {
   if (engineState == true)
        System.out.print("The engine is already on.");
   else {
       engineState = true;
        System.out.print("The engine is now on.");

   }

 void showAtts() {
   System.out.print("This motorcycle is a " + color + " " + make);
   if (engineState ==true)
       System.out.print("The engine is on.");
   else System.out.print("The engine is off.");

}
}

Seperate out the methods :

void startEngine() {
   if (engineState == true)
        System.out.print("The engine is already on.");
   else {
       engineState = true;
        System.out.print("The engine is now on.");
   }
}  // forgot this paranthesis


void showAtts() {
   System.out.print("This motorcycle is a " + color + " " + make);
   if (engineState ==true)
       System.out.print("The engine is on.");
   else System.out.print("The engine is off.");

}

Comments

2
class Motorcycle {


//Three instance variables - make and color are strings. while a boolean refers to TRUE OR FLASE(in this case off or on)
String make;
String color;
boolean engineState;

void startEngine() {
    if (engineState == true)
        System.out.print("The engine is already on.");
    else {
        engineState = true;
        System.out.print("The engine is now on.");

    }
    } //put one here

void showAtts() {
    System.out.print("This motorcycle is a " + color + " " + make);
    if (engineState ==true)
        System.out.print("The engine is on.");
    else System.out.print("The engine is off.");

}
}
// }  remove this 

Comments

1

It is in correct format without error try this one..

public class Motorcycle {

public static void main(String[] args) {
    Motorcycle s=new Motorcycle();
    s.showAtts();
    s.startEngine();
}

//Three instance variables - make and color are strings. while a boolean refers to TRUE OR FLASE(in this case off or on)
String make;
String color;
boolean engineState;

void startEngine() {
    if (engineState == true)
        System.out.println("The engine is already on.");
    else {
        engineState = true;
        System.out.println("The engine is now on.");

    }
}
void showAtts() {
    System.out.print("This motorcycle is a " + color + " " + make);
    if (engineState ==true)
        System.out.println("The engine is on.");
    else System.out.println("The engine is off.");

}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.