Results 1 to 13 of 13
  1. #1
    Gayethiri_86 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Exclamation Illegal start of expression error pls help me!

    Hi all,

    Ive written this code but, ive tried everything possible yet im getting this error message "illegal start of expression". This code is actually a modification of another code I wrote earlier. I had two issues with this code. Firstly I had to import:

    import pp.AntArt;

    However, JAVA did not regonise that and said that the package did not exist. I got rid of it and made other changes to the code and while doing so I got this error message for "illegal start of expression" I really need help urgently as I am really stuck with this for hours now and would greatly appreciate your comments. Thanks alot!

    p.s: Ive highlighted errors in red


    Code:
    import java.util.Scanner;
    import java.awt.Color;
    /**
    * Write a description of class stage2 here.
    *
    * @author (Gayethiri)
    * @version (version 1)
    */
    public class stage2
    {
    public static void main(String[]args)
    {
    final int WIDTH = 500; // width of the sheet in pixels
    final int HEIGHT = 500; // height of the sheet in pixels


    int x; // the x-coordinate
    int y; // the y-coordinate
    String direction; // the direction chosen


    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // 1. Ask the user for the ant's x-coordinate
    System.out.print("Enter your ant's x-coordinate: ");
    x = keyboard.nextInt();


    // 2. If the x-coordinate is outside the allowed range,
    // print an error message and exit
    if(x < 0 || x > WIDTH-1)
    {
    System.out.println("The x-coordinate must be between 0 and " + (WIDTH-1));
    System.exit(-1);
    }

    // 3. Ask the user for the ant's y-coordinate
    System.out.print("Enter your ant's y-coordinate: ");
    y = keyboard.nextInt();

    // 4. If the integer is outside the allowed range,
    // print an error message and exit
    if(y < 0 || y > HEIGHT-1)
    {
    System.out.println("The Y-coordinate must be between 0 and " + (HEIGHT-1));
    System.exit(-1);
    }


    // 5. Ask the user for the direction
    System.out.print("Enter the direction the ant is facing: ");
    direction = keyboard.next();

    System.out.print("How many steps should the ant take?: ");
    int numStepsToTake = keyboard.nextInt();

    for(int i=0; i<numStepsToTake;i++)
    {
    if (direction.equalsIgnoreCase("DL"))
    {
    y+=1;
    x-=1;
    }
    else if (direction.equalsIgnoreCase("DR"))
    {
    y-=1;
    x+=1;
    }
    else if (direction.equalsIgnoreCase("D"))
    {
    y-=1;
    }
    if (direction.equalsIgnoreCase("U"))
    {
    x+=1;
    }
    else if (direction.equalsIgnoreCase("UR"))
    {
    y+=1;
    x+=1;
    }
    else if (direction.equalsIgnoreCase("UL"))
    {
    y+=1;
    x-=1;
    }
    else if (direction.equalsIgnoreCase("L"))
    {
    x-=1;
    }
    else if(direction.equalsIgnoreCase("R"))
    {
    x+=1;
    }
    else
    {
    System.out.println("That is incorrect please enter the right direction");
    }

    // "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
    if(x < 0)
    {
    x += WIDTH;
    }
    else if(x > WIDTH-1)
    {
    x-=WIDTH;
    }
    if(y < 0)
    {
    y += HEIGHT;
    }
    else if(y > HEIGHT-1)
    {
    y-=HEIGHT;
    }

    //Here is the new method for antart.


    AntArt.init(WIDTH,HEIGHT,x,y);

    for(int i=0; i<numStepsToTake;i++)
    {
    int oldX = x;
    int oldY = y;

    Color paperColor = AntArt.getColor(x,y);
    //antColor = nextColor(antColor,paperColor);
    //AntArt.setColor(antColor);

    //Take a step
    //direction = nextDirection(direction,antColor,paperColor);

    x = stepX(x,direction,WIDTH,HEIGHT);
    y = stepY(y,direction,WIDTH,HEIGHT);

    AntArt.paint(oldX,oldY);

    }

    AntArt.paint(x,y);
    System.out.println("The new x is " + x + " and y coordinate is " + y);

    }

    private static int stepX(int x, String direction, int WIDTH, int HEIGHT)

    {
    // this will be the answer
    int newx = x;

    // take a step in the right direction
    if(direction.equals("UR"))
    {
    newx += 1;
    }
    else if (direction.equals("R"))
    {
    newx += 1;
    }
    else if(direction.equals("DR"))
    {
    newx += 1;
    }
    else if(direction.equals("DL"))
    {
    newx -= 1;
    }
    else if(direction.equals("L"))
    {
    newx -= 1;
    }
    else if(direction.equals("UL"))
    {
    newx -= 1;
    }

    // "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
    if(newx < 0)
    {
    newx += WIDTH;
    }
    else if(newx > WIDTH-1)
    {
    newx -= WIDTH;
    }

    return newx;
    }


    private static int stepY(int y, String direction, int WIDTH, int HEIGHT)
    {
    // this will be the answer
    int newy = y;

    // take a step in the right direction
    if(direction.equals("UR"))
    {
    newy += 1;
    }
    else if (direction.equals("R"))
    {
    newy += 1;
    }
    else if(direction.equals("DR"))
    {
    newy += 1;
    }
    else if(direction.equals("DL"))
    {
    newy -= 1;
    }
    else if(direction.equals("L"))
    {
    newy -= 1;
    }
    else if(direction.equals("UL"))
    {
    newy -= 1;
    }

    // "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
    if(newy < 0)
    {
    newy += HEIGHT;
    }
    else if(newy > HEIGHT-1)
    {
    newy -= HEIGHT;
    }

    return newy;
    }

    }



    Regards,
    Gayethiri

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Format your code with proper indenting and it should become clear why you are getting that error.

  3. #3
    Gayethiri_86 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default Hi there

    Thanks alot for your hint but, after proper indention I am still getting the same error message:confused: Please let me know what I am doing wrong over here. Also do you by any chance know why java isnt regonizing:

    import pp.AntArt;

    p.s: Im using bluej

    Regards,
    Gayethiri

    New Source code:
    import java.util.Scanner;
    import java.awt.Color;
    /**
    * Write a description of class stage2 here.
    *
    * @author (Gayethiri Guruthevan)
    * @version (Version 1.0)
    */
    public class stage2
    {
    public static void main(String[]args)
    {
    final int WIDTH = 500; // width of the sheet in pixels
    final int HEIGHT = 500; // height of the sheet in pixels


    int x; // the x-coordinate
    int y; // the y-coordinate
    String direction; // the direction chosen


    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // 1. Ask the user for the ant's x-coordinate
    System.out.print("Enter your ant's x-coordinate: ");
    x = keyboard.nextInt();


    // 2. If the x-coordinate is outside the allowed range,
    // print an error message and exit
    if(x < 0 || x > WIDTH-1)
    {
    System.out.println("The x-coordinate must be between 0 and " + (WIDTH-1));
    System.exit(-1);
    }

    // 3. Ask the user for the ant's y-coordinate
    System.out.print("Enter your ant's y-coordinate: ");
    y = keyboard.nextInt();

    // 4. If the integer is outside the allowed range,
    // print an error message and exit
    if(y < 0 || y > HEIGHT-1)
    {
    System.out.println("The Y-coordinate must be between 0 and " + (HEIGHT-1));
    System.exit(-1);
    }


    // 5. Ask the user for the direction
    System.out.print("Enter the direction the ant is facing: ");
    direction = keyboard.next();

    System.out.print("How many steps should the ant take?: ");
    int numStepsToTake = keyboard.nextInt();

    for(int i=0; i<numStepsToTake;i++)
    {
    if (direction.equalsIgnoreCase("DL"))
    {
    y+=1;
    x-=1;
    }
    else if (direction.equalsIgnoreCase("DR"))
    {
    y-=1;
    x+=1;
    }
    else if (direction.equalsIgnoreCase("D"))
    {
    y-=1;
    }
    if (direction.equalsIgnoreCase("U"))
    {
    x+=1;
    }
    else if (direction.equalsIgnoreCase("UR"))
    {
    y+=1;
    x+=1;
    }
    else if (direction.equalsIgnoreCase("UL"))
    {
    y+=1;
    x-=1;
    }
    else if (direction.equalsIgnoreCase("L"))
    {
    x-=1;
    }
    else if(direction.equalsIgnoreCase("R"))
    {
    x+=1;
    }
    else
    {
    System.out.println("That is incorrect please enter the right direction");

    }

    // "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
    if(x < 0)
    {
    x += WIDTH;
    }
    else if(x > WIDTH-1)
    {
    x-=WIDTH;
    }
    if(y < 0)
    {
    y += HEIGHT;
    }
    else if(y > HEIGHT-1)
    {
    y-=HEIGHT;
    }

    //Here is the new method for antart.


    AntArt.init(WIDTH,HEIGHT,x,y);

    for(int i=0; i<numStepsToTake;i++)
    {
    int oldX = x;
    int oldY = y;

    Color paperColor = AntArt.getColor(x,y);
    antColor = nextColor(antColor,paperColor);
    AntArt.setColor(antColor);

    // Take a step
    direction = nextDirection(direction,antColor,paperColor);

    x = stepX(x,direction,WIDTH,HEIGHT);
    y = stepY(y,direction,WIDTH,HEIGHT);

    AntArt.paint(oldX,oldY);

    }

    AntArt.paint(x,y);
    System.out.println("The new x is " + x + " and y coordinate is " + y);

    }



    private static int stepX(int x, String direction, int WIDTH, int HEIGHT) {
    //this will be the answer
    int newx = x;
    // take a step in the right direction
    if(direction.equals("UR"))
    {
    newx += 1;
    }
    else if (direction.equals("R"))
    {
    newx += 1;
    }
    else if(direction.equals("DR"))
    {
    newx += 1;
    }
    else if(direction.equals("DL"))
    {
    newx -= 1;
    }
    else if(direction.equals("L"))
    {
    newx -= 1;
    }
    else if(direction.equals("UL"))
    {
    newx -= 1;
    }

    // "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
    if(newx < 0)
    {
    newx += WIDTH;
    }
    else if(newx > WIDTH-1)
    {
    newx -= WIDTH;
    }

    return newx;
    }





    private static int stepY(int y, String direction, int WIDTH, int HEIGHT)
    {
    // this will be the answer
    int newy = y;

    // take a step in the right direction
    if(direction.equals("UR"))
    {
    newy += 1;
    }
    else if (direction.equals("R"))
    {
    newy += 1;
    }
    else if(direction.equals("DR"))
    {
    newy += 1;
    }
    else if(direction.equals("DL"))
    {
    newy -= 1;
    }
    else if(direction.equals("L"))
    {
    newy -= 1;
    }
    else if(direction.equals("UL"))
    {
    newy -= 1;
    }

    // "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
    if(newy < 0)
    {
    newy += HEIGHT;
    }
    else if(newy > HEIGHT-1)
    {
    newy -= HEIGHT;
    }

    return newy;
    }

    }

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Most likely you are missing a closing brace before that line in red.

  5. #5
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default

    Correct indentation does not resolve the issues. But definitely it gives a better looks to code and better readability, also put the code in code tags. Now as far as you error is concerned, probably you are starting the definition of private static int stepX method before giving the closing bracket of main method.
    Swastik

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by dswastik View Post
    Correct indentation does not resolve the issues...
    No one said that it does.
    Rather that it makes it clear what the problem is.
    If you indent properly you are able to easily see that your opening and closing braces do not match by visually aligning them.

  7. #7
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default

    Sir,

    This was just to clarify the doubt

    Thanks alot for your hint but, after proper indention I am still getting the same error message
    Swastik

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by dswastik View Post
    Sir,

    This was just to clarify the doubt
    I see what you meant. The OP still has not indented their code (or maybe it's because they didn't post it with code tags).

    P.S If you call me "Sir" again I'll set the defenestration authorities upon you.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by r035198x View Post
    P.S If you call me "Sir" again I'll set the defenestration authorities upon you.
    Hello Miss, did you ask for a nice and clean head-first defenestration? Shall I put the cars out of the way Miss?

    kind regards,

    Jos ;-)

  10. #10
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default

    Sorry about that.
    Swastik

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Beheadings? Did anyone mention beheadings?

    Oooh, never mind..
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhHein View Post
    Beheadings? Did anyone mention beheadings?

    Oooh, never mind..
    No, nobody mentioned beheadings; we are no barbarians; we only do nice and clean head-first defenestrations, not bloody beheadings, you silly. We do drop the carcass on pointy sticks though but the peasants and other cannibals'll take care of that ...

    kindest regards,

    Jos ;-)

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

Similar Threads

  1. Illegal start of expression error
    By lukermsdn111 in forum New To Java
    Replies: 9
    Last Post: 03-22-2010, 02:31 PM
  2. Illegal start of expression
    By Basit56 in forum New To Java
    Replies: 2
    Last Post: 08-18-2009, 10:12 AM
  3. Servlet Error -illegal start of expression
    By raghu9198 in forum Java Servlet
    Replies: 2
    Last Post: 04-22-2009, 12:12 AM
  4. Illegal Start of an Expression
    By ddatta8 in forum New To Java
    Replies: 3
    Last Post: 12-20-2008, 09:40 PM
  5. illegal start of expression
    By razmyasdfg in forum CLDC and MIDP
    Replies: 2
    Last Post: 07-27-2008, 11:44 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
  •