Results 1 to 5 of 5
  1. #1
    WhopperMan is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default 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);
    }
    }
    }

  2. #2
    the
    the is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default

    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);
    }

    }

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    and make the drawSquare method static

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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 03:52 AM.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    You are presuming TurtleArt is a proper class which I reckon it is not, just a name for a class that includes only a static run method.

    If turtleArt is supposed to be a class (with methods and functionality) I would get rid of the static main() and use it in some other class e.g runTurtleArt

Similar Threads

  1. Illegal Start of expression
    By Macca07 in forum New To Java
    Replies: 3
    Last Post: 11-23-2009, 09:43 AM
  2. Illegal start of expression
    By Basit56 in forum New To Java
    Replies: 2
    Last Post: 08-18-2009, 10:12 AM
  3. Illegal Start of an Expression
    By ddatta8 in forum New To Java
    Replies: 3
    Last Post: 12-20-2008, 09:40 PM
  4. illegal start of expression
    By razmyasdfg in forum CLDC and MIDP
    Replies: 2
    Last Post: 07-27-2008, 11:44 PM
  5. Illegal start of expression
    By gabriel in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 06:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •