0

Here is my code:

public void start()
{
  //If the gas tank and oil have more than nothing, and the transmission is in gear park, then the car can start
  if((gasTank.getGasLevel() > 0) && (engine.getOilLevel() > 0) && (transmission.gearPark()))
  {
    engine.startEngine();
    System.out.println("Engine is now on");
  }
  else
  {
    System.out.println("Please make sure your car is in proper gear, engine has oil and gas.");
  }
}

I keep getting 'void' type not allowed here error. I am not trying to return anything so I know I don't need int, Boolean, double, etc.

What am I doing wrong?

9
  • Where do you get that error?
    – Jochen
    Feb 22, 2013 at 8:21
  • 3
    Can you post the entire class? I think it is likely some typo or missing curly braces
    – Hari Menon
    Feb 22, 2013 at 8:23
  • 12
    Is the name of your class start by any chance?
    – Ash
    Feb 22, 2013 at 8:23
  • 2
    I think one of your method return type in if statement is void Feb 22, 2013 at 8:23
  • 1
    Paste the exact and complete error message, and tell us which line in your code it refers to.
    – JB Nizet
    Feb 22, 2013 at 8:24

2 Answers 2

7

Your code is not inside a class.

Try this:

public class SomeClass {
    // your method here
}
6
  • +1 - This is probably the issue. But without more code we cant be certain
    – cowls
    Feb 22, 2013 at 8:33
  • This seems to be a legit reason!!
    – codeMan
    Feb 22, 2013 at 8:34
  • I've been getting downvotes too, for no reason as far as I can see. I suspect a troll.
    – NickJ
    Feb 22, 2013 at 8:35
  • Correct. without an enclosing class, he code gives: Illegal type: void, 'class' or 'interface' expected. Feb 22, 2013 at 8:43
  • 1
    Sorry!! This was for an assignment a friend had helped me resolve the issue shortly after posted.. this was the answer!
    – BRBT
    Feb 10, 2014 at 20:43
1

It looks like one or more of these methods might be void, but must not be:

getGasLevel(); 
getOilLevel();
gearPark()

They must not be void because you are checking values returned from them in the if statement.

3
  • Why mark this down? Leave a comment explaining what's wrong with the answer
    – NickJ
    Feb 22, 2013 at 8:26
  • I think this is a valid answer... Feb 22, 2013 at 8:32
  • 2
    This does not give the error the OP was experiencing... Simple example: public static void main(String args[]) throws Exception { if(getInt() > 0) { } } public static void getInt() { } it gives the error: the operator is undefined for the argument types, void, int This is probably why you got downvotes.
    – cowls
    Feb 22, 2013 at 17:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.