File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Gets error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Gets error "Illegal start of expression"" Watch "Gets error "Illegal start of expression"" New topic
Author

Gets error "Illegal start of expression"

Rameez Shaik
Greenhorn

Joined: Sep 25, 2010
Posts: 17



whats is causing this error??
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
Pleasy use code tag when posting code.

Your applyBrake() method is inside your main method.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Rameez Shaik
Greenhorn

Joined: Sep 25, 2010
Posts: 17
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

Joined: Nov 18, 2010
Posts: 12
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

Joined: Apr 15, 2008
Posts: 198
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Sep 25, 2010
Posts: 17


Now this time i am getting error as "Variable isMoving cannot be referred from static context"
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Nov 18, 2010
Posts: 12
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

Joined: Sep 25, 2010
Posts: 17
Thank you all ..finally got the answer
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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

Joined: Nov 21, 2010
Posts: 26
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..!");
}
}
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Gets error "Illegal start of expression"