Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Illegal Start of Expression

    Very new to Java and trying to build a pyramid building program. I get an error on line 15:

    private void buildBricks() {

    that says Illegal Start of Expression with no more helpful information as to what that means. Just wondering if anyone could point me in the right direction!

    Don't worry about the rest of the code, I can troubleshoot that once it actually let's me build and run it, just need help declaring this method for now.

    Thanks so much!

    // This project should build a pyramid with a base of 14 blocks
    // of block size 12 x 30 pixels. The pyramid should be centered in the screen.

    package pleasework;
    import acm.graphics.*;
    import acm.program.*;

    public class buildBricks extends GraphicsProgram {
    private final int BRICK_WIDTH = 30;
    private final int BRICK_HEIGHT = 12;
    private final int BRICKS_IN_BASE = 14;

    public static void main(String[] args) {

    private void buildBricks() {
    int screenW = getHeight();
    for(int i=0;i==BRICKS_IN_BASE;i++) {
    for(int j = 0; j > ((BRICKS_IN_BASE - 1) - i);j++) {
    int blockPosition = screenW/14 + ((j/2) * BRICK_WIDTH);
    int rowHeight = (BRICKS_IN_BASE * BRICK_HEIGHT) * (BRICK_HEIGHT * i);
    GRect rect = new GRect(blockPosition,rowHeight,BRICK_WIDTH,BRICK_HE IGHT);
    add(rect);
    blockPosition = blockPosition + BRICK_WIDTH;
    }
    }

    GRect rect1 = new GRect (210,168,30,12);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're nesting a method inside of another method, something not allowed in Java (not directly). Solution: don't do this. Get your buildBricks method completely outside of the main method.

    Also, please use code tags when posting code, not quote tags as your code is then difficult to read.

  3. #3
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    wow that was very simple, thank you so much!

    I just ran it and everything went smoothly, but how do I get the results to show up on the screen?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Tyre View Post
    wow that was very simple, thank you so much!

    I just ran it and everything went smoothly, but how do I get the results to show up on the screen?

    I'm not sure, as for one I still can't read your code as you've not edited it yet and changed your [quote] [/quote] tags to [code] [/code] tags. You may consider doing this.

  5. #5
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Using Netbeans 6.9.1 by the way.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Tyre View Post
    Using Netbeans 6.9.1 by the way.
    This matters little. Please see my recommendation above.

  7. #7
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Sorry, this should be it:

    Java Code:
    // This project should build a pyramid with a base of 14 blocks
    // of block size 12 x 30 pixels. The pyramid should be centered in the screen.
    
    package pleasework;
    import acm.graphics.*;
    import acm.program.*;
    
    public class buildBricks extends GraphicsProgram {
        private final int BRICK_WIDTH = 30;
        private final int BRICK_HEIGHT = 12;
        private final int BRICKS_IN_BASE = 14;
    
        public static void main(String[] args) {
        }
             private void buildBricks() {
                int screenW = getHeight();
                for(int i=0;(i==BRICKS_IN_BASE - 1);i++) {
    
    //starts a for loop that runs as many times as their are bricks in the base
    
                    for(int j = 0; j > ((BRICKS_IN_BASE - 1) - i);j++) {
                        
    //for loop to create a line counting down from bricks in base to one
    
                        int blockPosition = screenW/14 + ((j/2) * BRICK_WIDTH);
                        int rowHeight = (BRICKS_IN_BASE * BRICK_HEIGHT) * (BRICK_HEIGHT * i);
                        GRect rect = new GRect(blockPosition,rowHeight,BRICK_WIDTH,BRICK_HEIGHT);
                        add(rect);
                        blockPosition = blockPosition + BRICK_WIDTH;
                        }
                    }
        }
    
    }

  8. #8
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Also I had tried adding the line

    buildBricks();

    within the main but it says that non static method buildBricks cannot be referenced from a static context.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It's a class method so you need an instance of the class to call it on in main.

    Java Code:
    main
      Create class instance
      Call method on class instance 
    End main

  10. #10
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    I'm sorry I am not really understanding. I have created the method and have tried calling it in the main function via buildBricks(); but it gives me errors about static versus non-static.

    What code is it looking for?

    Sorry to be so useless, it's just frustrating to want to debug but being unable to because of these things. I just want to get something to work already!

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Read the reply above yours by Sunde.

  12. #12
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Yes I do not understand what is wrong with my code. I'm sorry, class and instance are running together and I don't know what I've done wrong. I entered the calling of the method into the main class and so it looks like this:

    Java Code:
    public class buildBricks extends GraphicsProgram {
        private final int BRICK_WIDTH = 30;
        private final int BRICK_HEIGHT = 12;
        private final int BRICKS_IN_BASE = 14;
    
        public static void main(String[] args) {
            buildBricks();
        
        }
             private void buildBricks() {
                int screenW = getHeight();
                for(int i=0;(i==BRICKS_IN_BASE - 1);i++) {
    
    //starts a for loop that runs as many times as their are bricks in the base
    
                    for(int j = 0; j > ((BRICKS_IN_BASE - 1) - i);j++) {
                        
    //for loop to create a line counting down from bricks in base to one
    
                        int blockPosition = screenW/14 + ((j/2) * BRICK_WIDTH);
                        int rowHeight = (BRICKS_IN_BASE * BRICK_HEIGHT) * (BRICK_HEIGHT * i);
                        GRect rect = new GRect(blockPosition,rowHeight,BRICK_WIDTH,BRICK_HEIGHT);
                        add(rect);
                        blockPosition = blockPosition + BRICK_WIDTH;
                        }
                 
                }
        }
    
    }
    Where exactly did I go wrong?

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Ok maybe this might make you think.
    Java Code:
    class Foo {
        public static void main(String[] args) {
            buildBricks();
        }
    }
    Why should your code work but mine won't?

    Anther point to ponder. Suppose you have a String "hello" and you wnat to display the length: 5.
    Java Code:
    System.out.println(length());
    Where is the length method? Whose length is it supposed to print?

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can either make the method static and call it in main, or non static and call the method on the instance the method belongs to. To get the substring of some string you need to create the actual string first.

    Java Code:
    String s = "hi there";
    s.substring(2);
    Similarly you need to create an instance of your class before calling this method
    Java Code:
    Snip, junkies help makes it so I can't justify spoon feeding
    the first line creates an instance of the class, the second line calls a method on that object you just created.

    I gave this to you only because static and non static problems tend to be hard to grasp when people are new to java. You should read up on the two for more understanding of what the error message meant.

    One final thing, classes should have the first letter of each word capitalized and methods should only have the first letter of every letter after the first word capitalized
    Java Code:
    class AClass
    class AnotherClass
    
    public void aMethod()
    public void anotherMethid()
    Last edited by sunde887; 04-15-2011 at 01:39 AM.

  15. #15
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Ahhh this is tantalizingly close, or I'm just not getting it. So I need to tell it what thing to send out?

    Now I only have one red squiggly under rect in
    Java Code:
      System.out.buildPyramid(rect);
    That just won't go away. I don't understand what you mean with the substrings, but making the method static just puts the entire method in squiggly death.
    Java Code:
    // This project should build a pyramid with a base of 14 blocks
    // of block size 12 x 30 pixels. The pyramid should be centered in the screen.
    
    package pleasework;
    import acm.graphics.*;
    import acm.program.*;
    
    public class buildBricks extends GraphicsProgram {
        private final int BRICK_WIDTH = 30;
        private final int BRICK_HEIGHT = 12;
        private final int BRICKS_IN_BASE = 14;
    
        public static void main(String[] args) {
            System.out.buildPyramid(rect);
      
        }
            
            private void BuildPyramid() {
        
    
                int screenW = getHeight();
                for(int i=0;(i==BRICKS_IN_BASE - 1);i++) {
    
    //starts a for loop that runs as many times as their are bricks in the base
    
                    for(int j = 0; j > ((BRICKS_IN_BASE - 1) - i);j++) {
                        
    //for loop to create a line counting down from bricks in base to one
    
                        int blockPosition = screenW/14 + ((j/2) * BRICK_WIDTH);
                        int rowHeight = (BRICKS_IN_BASE * BRICK_HEIGHT) * (BRICK_HEIGHT * i);
                        GRect rect = new GRect(blockPosition,rowHeight,BRICK_WIDTH,BRICK_HEIGHT);
                        add(rect);
                        blockPosition = blockPosition + BRICK_WIDTH;
                        }
                 }
                }
    }

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Think of junkies example, what do you need to do first in order to print the length of a string?

  17. #17
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Oh shit you have to create it. Now I see what he means, I just thought that when you went down and made the method that it existed.

    Does creating new methods work the same as, say, ovals?

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How would you create junkies example to work? Put it in code tags. Strings are a weird case since you won't be creating it with the constrictor and instead will use a strig literal as a right hand argument. You want to create an object of type buildBrick.

  19. #19
    Tyre is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Okay well I haven't gotten to strings in java but I've worked with them a little in Visual Basic. I guess to measure a string I would:
    Java Code:
    String hello = "hello";
            int len = hello.length();
            println("Length = " + len);
    I actually just wrote that into a program and it gave me the same static/non-static thing. Is the code itself wrong?

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You want to use System.out.println.

    So you have now constructed a string and called a method on it.

    What you want to do for your original problem is construct an object of type buildBricks and then call a method on it. It works very similar to your above string code. You use a default constructor which is available to you right now.

    If you still can't figure it out try creating some other objects to getthe hang of using a constructor.

    Create a scanner, string builder, point, and JFrame. Post the code, this should help you understand how to create types of your own class.

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help with illegal start of expression
    By WhopperMan in forum New To Java
    Replies: 4
    Last Post: 10-10-2010, 02:58 AM
  2. Illegal Start of expression
    By Macca07 in forum New To Java
    Replies: 3
    Last Post: 11-23-2009, 08:43 AM
  3. Illegal Start of an Expression
    By ddatta8 in forum New To Java
    Replies: 3
    Last Post: 12-20-2008, 08:40 PM
  4. Illegal Start of Expression
    By vince425 in forum New To Java
    Replies: 3
    Last Post: 10-18-2008, 07:41 AM
  5. Illegal start of expression
    By gabriel in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05: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
  •