• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Gets error "Illegal start of expression"

 
Rameez Shaik
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



whats is causing this error??
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pleasy use code tag when posting code.

Your applyBrake() method is inside your main method.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you edit your post so it uses code tags ([code][/code]). The problem looks like you are trying to define the void applyBrake() method inside the main() method - which you can't do.
 
Rameez Shaik
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to write the IF and Else condition inside applyBrake() method and then call it from main.
Can i do it by creating only one class??
 
Markus Olsson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can, simple call the applyBreak method from the main method, dont place it there.
If you put the method outside the main method (but still in the same class) like this:


*Edit*
Yes, I was planning on explaining how that works when he understood the problem that will come
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

First of all move your applyBrake out of your main method. Then you can either move the variables CurrentSpeed (by the way, use lowercase with variable names i.e. currentSpeed) and isMoving to class level from the main method or provide them as method arguments to applyBreak method. Moving the variables to class level is a better solution for isMoving.

EDIT: Markus Olsson was faster But his answer did not take into account the variables used.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure you make the 'applyBrake()' method static - though. The main method is static and you don't make an instance of the Car class. This means you are in a 'static context' and the applyBrakes method needs to also be static.

Once you get comfortable calling methods you will learn about instances, and what not - and then you can worry about doing it right :-)
 
Rameez Shaik
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now this time i am getting error as "Variable isMoving cannot be referred from static context"
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rameez Shaik wrote:
Now this time i am getting error as "non static method applyBrake() cannot be referred from a static context"


The solution should be rather obvious from the message. Also read my last post. Also - the same will apply for the two variable you define.
 
Markus Olsson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To learn more about static and understanding why you won't be able to do like that, read this:
http://dadicy.wordpress.com/2007/10/29/what-do-you-mean-by-static-in-java/
 
Rameez Shaik
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all ..finally got the answer
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have found your original error much faster if you had indented your code and {} correctly. When writing a block do it in stages like thisIt isn't the way you are used to writing, because you are used to writing ordinary text, but in code the rules are different, so you would do well to learn a different way to do it.
 
Pawan Kr Gupta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below code will run


public class Car
{
static int CurrentSpeed = 2;
static boolean isMoving = true;

public static void main(String[] args)
{

applyBrake();


}

public static void applyBrake()
{
if(isMoving)
{
CurrentSpeed--;
}
else
{
System.err.println("The bicycle has already stopped..!");
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic