Results 1 to 5 of 5
- 10-10-2010, 02:39 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
Need help with illegal start of expression
I'm trying to start an assignment, and am trying to create methods, but for some reason it keeps giving me the error, illegal start of expression. Any help is appreciated. The error is the line in red text.
import java.awt.Color;
public class TurtleArt
{
public static void main (String[] args)
{
public void drawSquare(int width)
{
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
}
}
}
- 10-10-2010, 02:45 AM #2
Member
- Join Date
- Oct 2010
- Posts
- 1
- Rep Power
- 0
Hi, you could try something like this instead (move the drawSquare method out of your main method):
import java.awt.Color;
public class TurtleArt
{
public static void main (String[] args)
{
// Make a call to your drawSquare method here
}
public void drawSquare(int width)
{
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
}
}
- 10-10-2010, 02:48 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
and make the drawSquare method static
- 10-10-2010, 02:49 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
You can't nest methods in Java like that.
The main() method must be completely finished down to the closing } before you start on the drawSquare() method.
So I agree with the, but would add that it's not as simple as calling drawSquare() within the main method: you have to create an instance of TurtleArt first and then call its drawSquare. Resist the impule to make drawSquare() static.
-----
[Edit]
I've just seen the post above mine.Last edited by pbrockway2; 10-10-2010 at 02:52 AM.
- 10-10-2010, 02:58 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
Illegal Start of expression
By Macca07 in forum New To JavaReplies: 3Last Post: 11-23-2009, 08:43 AM -
Illegal start of expression
By Basit56 in forum New To JavaReplies: 2Last Post: 08-18-2009, 09:12 AM -
Illegal Start of an Expression
By ddatta8 in forum New To JavaReplies: 3Last Post: 12-20-2008, 08:40 PM -
illegal start of expression
By razmyasdfg in forum CLDC and MIDPReplies: 2Last Post: 07-27-2008, 10:44 PM -
Illegal start of expression
By gabriel in forum New To JavaReplies: 2Last Post: 08-01-2007, 05:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks