Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 24 of 24

Thread: Java error "Another <identifier> expected"

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Red face Java error "Another <identifier> expected"

    So I'm writing a program for a puzzle for my University Lab, but I run into a problem when I try to input 2 integers when constructing. Here's my full code (not nearly completed but still, I work in a one step at a time fashion..rather frustrating at times)
    simpleCanvas is simply a graphic interface for Java. There are no problems with it, I believe it was created by the BlueJ authors or something? Anyway, any help is greatly appreciated Thanks in advance!

    Site for lab work is: Java Programming (CITS1200)

    public class FifteenPuzzle  {
     
            //Instance Variables
            public SimpleCanvas sc;
     
            //Constructor
            public FifteenPuzzle (int[][] intialGrid){
                sc = new SimpleCanvas ();
     
     
     
                //Colour
                java.awt.Color tBlue = new java.awt.Color(0,0,255);
                sc.setForegroundColour(tBlue);
                for (int i=0; i < 400;i++) sc.drawLine(0,i,400,i);
     
                // Lines
                java.awt.Color black = new java.awt.Color(0,0,0);
                sc.setForegroundColour(black);
                sc.drawLine(100,0,100,400);
                sc.drawLine(200,0,200,400);
                sc.drawLine(300,0,300,400);
                sc.drawLine(0,100,400,100);
                sc.drawLine(0,200,400,200);
                sc.drawLine(0,300,400,300); 
                //Lines
     
                //Input
     
            }
     
            //Methods
            public void moveTile (int x, int y){
            }
     
            //public int [][] getCurrentGrid(){
            //}
     
     
        }
    Last edited by Deep_4; November 7th, 2012 at 10:38 PM. Reason: retarded question


  2. #2
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Another <identifier> expected problem...

    I've worked on it a little more, but I'm still getting an <identifier> problem...please, any help is very very much appreciated

    New Code:
    public class FifteenPuzzle  {
     
            //Instance Variables
            public SimpleCanvas sc;
            public int x;
            public int y;
     
            //Constructor
            public FifteenPuzzle(int[][] initialGrid){
                sc = new SimpleCanvas ();
                x = initialGrid.length;
                y = initialGrid[x].length;
     
                //Colour
                java.awt.Color tBlue = new java.awt.Color(0,0,255);
                sc.setForegroundColour(tBlue);
                for (int i=0; i < 400;i++) sc.drawLine(0,i,400,i);
     
                // Lines
                java.awt.Color black = new java.awt.Color(0,0,0);
                sc.setForegroundColour(black);
                sc.drawLine(100,0,100,400);
                sc.drawLine(200,0,200,400);
                sc.drawLine(300,0,300,400);
                sc.drawLine(0,100,400,100);
                sc.drawLine(0,200,400,200);
                sc.drawLine(0,300,400,300); 
                //Lines
     
                //Input
                java.awt.Color white = new java.awt.Color(255,255,255);
                sc.setForegroundColour(white);
                for (int i=0; i<(x * 100);i++) sc.drawLine((y*100),i,(x*100),i);
     
            }
     
            //Methods
            public void moveTile (int x, int y){
            }
     
            //public int [][] getCurrentGrid(){
            //}
     
     
        }

    SimpleCanvas:
    /**
     * This is a stripped-down version of the Canvas class from the 
     * BlueJ team, retaining only the most fundamental features.
     * 
     * @author BlueJ team with modifications by Gordon Royle 
     * @version July 2003
     */
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class SimpleCanvas
    {
        private JFrame frame;
        private CanvasPane canvas;
        private Graphics2D graphic;
        private Image canvasImage;
        private boolean autoRepaint;
     
     
        /**
         * Creates and displays a SimpleCanvas of the specified size
         * with a white background. The client specifies whether repainting
         * after a drawing command should be manual or automatic.
         * 
         * @param   title title for the window
         * @param   width the desired width of the SimpleCanvas 
         * @param   height the desired height of the SimpleCanvas
         * @param   autoRepaint true for automatic repainting
         * 
         */
     
        public SimpleCanvas(String title, int width, int height, boolean autoRepaint) {
            frame = new JFrame();
            canvas = new CanvasPane();
            frame.setContentPane(canvas);
            frame.setTitle(title);
            canvas.setPreferredSize(new Dimension(width,height));
            frame.pack();
            Dimension size = canvas.getSize();
            canvasImage = canvas.createImage(size.width,size.height);
            graphic = (Graphics2D) canvasImage.getGraphics();
            graphic.setColor(Color.white);
            graphic.fillRect(0,0,size.width,size.height);
            graphic.setColor(Color.black);
            frame.setVisible(true);
            frame.setVisible(true);
    	//        frame.show();
            frame.setVisible(true);
            this.autoRepaint = autoRepaint;
        }
     
       /**
         * Creates and displays a SimpleCanvas with a white background and
         * with automatic repainting after drawing commands.
         * 
         * @param   title title for the window
         * @param   width the desired width of the SimpleCanvas 
         * @param   height the desired height of the SimpleCanvas
         * 
         */
     
        public SimpleCanvas(String title, int width, int height) {
            this(title,width,height,true);
        }
     
        /**
         * Creates and displays a SimpleCanvas of size 400x400 with the
         * default title "SimpleCanvas" and with automatic repainting 
         * enabled.
         * 
         * @param   title title for the window
         * @param   width the desired width of the SimpleCanvas 
         * @param   height the desired height of the SimpleCanvas
         * 
         */
     
        public SimpleCanvas() {
            this("SimpleCanvas",400,400);
     
     
            }
     
        /** 
         * Draws a line on the SimpleCanvas between two points.
         * 
         * @param   x1 x-coordinate of the first point
         * @param   y1 y-coordinate of the first point
         * @param   x2 x-coordinate of the second point
         * @param   y2 y-coordinate of the second point
         *
         */
     
        public void drawLine(int x1, int y1, int x2, int y2) {
            graphic.drawLine(x1,y1,x2,y2);
            if (autoRepaint) canvas.repaint();
        }
     
        /** 
         * Changes the colour for subsequent 
         * drawing on this SimpleCanvas.
         * 
         * @param   newColour the new drawing colour
         * 
         */
     
        public void setForegroundColour(Color newColour) {
            graphic.setColor(newColour);
        }
     
        /**
         * Gets the colour currently used for 
         * drawing on this SimpleCanvas.
         * 
         */
     
        public Color getForegroundColour() {
            return graphic.getColor();
        }
     
        /**
         * Changes the font for subsequent String
         * drawing on this SimpleCanvas.
         *
         * @param   newFont the new Font
         * 
         */
     
        public void setFont(Font newFont) {
            graphic.setFont(newFont);
        }
     
        /**
         * Gets the font currently used for
         * String drawing on this Canvas
         */
     
        public Font getFont() {
            return graphic.getFont();
        }
     
        /**
         * Draws the specified String at the specified
         * location on this SimpleCanvas
         */
     
        public void drawString(String text, int x, int y) {
            graphic.drawString(text, x, y);
            if (autoRepaint) canvas.repaint();
        }
     
        /**
         * Sets the repaint mode to either manual or automatic.
         * 
         * @param   autoRepaint automatic repainting if this is true
         * 
         */
     
        public void setAutoRepaint(boolean autoRepaint) {
            this.autoRepaint = autoRepaint;
        }
     
     
        /**
         * If this SimpleCanvas does not automatically repaint 
         * after each drawing command, then this method can be
         * used to cause a manual repaint.
         */
     
        public void repaint() {
            canvas.repaint();
        }
     
        /**
         * Causes execution to pause for the specified amount of time.
         * This is usually used to produce animations in an easy
         * manner, by repeatedly drawing, pausing, and then redrawing
         * an object.
         */
     
        public void wait(int millis) {
            try {
                Thread.sleep(millis);
            } catch (InterruptedException ie) {
                System.out.println("Interrruption in SimpleCanvas: "+ie);
            }
        }
     
        public void addMouseListener(MouseListener ml) {
            canvas.addMouseListener(ml);
        }
     
        class CanvasPane extends JPanel {
            public void paint(Graphics g) {
                g.drawImage(canvasImage,0,0,null);
            }
        }
     
    }
    Last edited by bruint; May 1st, 2009 at 01:45 AM. Reason: Added SimpleCanvas code

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Another <identifier> expected problem...

    Without all of the code, we cannot compile and run it. So if you can please post all of the code including SimpleCanvas class. If you cannot do that for some reason then please Post the exact error message you get and where it occurs and tell us what it should do and what it is doing.

    Regards,
    Chris

  4. #4
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Another <identifier> expected problem...

    Thanks Chris, I've posted the SimpleCanvas code as requested.

  5. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Another <identifier> expected problem...

    You'll have to excuse me for a couple of hours as I have to go to college. When I am there and have a bit of spare time I'll get back to you hopefully with a solution

    chris

  6. #6
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Another <identifier> expected problem...

    Thanks very much. Any help is greatly appreciated Hope to hear from you soon.

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Another <identifier> expected problem...

    Hello bruint,

    For some reason I cannot get this code to compile? There are no errors but I cannot get it to run in Eclipse.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. #8
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Another <identifier> expected problem...

    I'm compiling in BlueJ fine. They are 2 seperate classes (as im sure you already know) which class in particular is not compiling?

  9. #9
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Another <identifier> expected problem...

    Hi, I do not understand your problem exactly, but I think this may be the problem.
    x = initialGrid.length;
    y = initialGrid[x].length;
    initialGrid[x] is out of bouds, the upper bound to initialGrid[] is x-1. So you should replace it and use this,
    x = initialGrid.length;
    y = initialGrid[x-1].length;

    It then runs fine, well as far as i can tell

    Regards,
    Chris
    Last edited by Freaky Chris; May 1st, 2009 at 04:31 AM.

  10. #10
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Another <identifier> expected problem...

    Quote Originally Posted by JavaPF View Post
    Hello bruint,

    For some reason I cannot get this code to compile? There are no errors but I cannot get it to run in Eclipse.
    BlueJ allows you to specify the program entry point. Thus no main function is required.

    Add this to the FifteenPuzzle class,
    public static void main(String[] args){
    	new FifteenPuzzle(new int[1][2]);
    }

    Regards,
    Chris

  11. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: Another <identifier> expected problem...

    Quote Originally Posted by Freaky Chris View Post
    BlueJ allows you to specify the program entry point. Thus no main function is required.

    Add this to the FifteenPuzzle class,
    public static void main(String[] args){
        new FifteenPuzzle(new int[1][2]);
    }
    Regards,
    Chris
    Good man Chris, that allowed me to compile it. I wonder if there is a way in Eclipse to select the entry point?!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  12. #12
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Another <identifier> expected problem...

    Chris, I've edited my code as you said. But Im still getting the identifier problem when I try to run my own constructor (A requirement for my lecturer - he marks with an automated script)
    Your constructor runs fine but I simple can't use it. Perhaps I'm putting it in the wrong place? here's how my code looks now:
    public class FifteenPuzzle  {
     
            //Instance Variables
            public SimpleCanvas sc;
            public int x;
            public int y;
            public int[][] initialGrid;
    [B]        public static void main(String[] args){
            new FifteenPuzzle(new int[1][2]);
            }[/B]
     
            //Constructor
            public FifteenPuzzle(int[][] initialGrid){
                sc = new SimpleCanvas ();
                x = initialGrid.length;
                y = initialGrid[x-1].length;
     
                //Colour
                java.awt.Color tBlue = new java.awt.Color(0,0,255);
                sc.setForegroundColour(tBlue);
                for (int i=0; i < 400;i++) sc.drawLine(0,i,400,i);
     
                // Lines
                java.awt.Color black = new java.awt.Color(0,0,0);
                sc.setForegroundColour(black);
                sc.drawLine(100,0,100,400);
                sc.drawLine(200,0,200,400);
                sc.drawLine(300,0,300,400);
                sc.drawLine(0,100,400,100);
                sc.drawLine(0,200,400,200);
                sc.drawLine(0,300,400,300); 
                //Lines
     
                //Input
                java.awt.Color white = new java.awt.Color(255,255,255);
                sc.setForegroundColour(white);
                for (int i=0; i<(x * 100);i++) sc.drawLine((y*100),i,(x*100),i);
     
            }
     
            //Methods
            public void moveTile (int x, int y){
            }
     
            //public int [][] getCurrentGrid(){
            //}
     
     
        }

    And here's a screenshot of the problem:

  13. #13
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Another <identifier> expected problem...

    Hmm... I don't get this error? Does it appear when you compile the code?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  14. #14
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Another <identifier> expected problem...

    No, it occurs after compiling. I'm not exactly what the proccess is called, but on a guess, it's sort of like invoking a constructor from a class, and then the user can input the numbers, string, characters they want for a particular constructor. If that makes sense?

  15. #15
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Another <identifier> expected problem...

    bruint, you do not require
    public static void main(String[] args){
            new FifteenPuzzle(new int[1][2]);
            }
    That section was ment for JavaPF. You only needed to make the x to x-1 change that I showed you. Which you have now done.

    I suspect this error message is being thrown by BlueJ because you are trying to pass 2 integers as the arguments to the constructor, not a 2D array!
    I have no idea how BlueJ Works, but perhaps something like this, {{0},{0, 0}}....Probably wont work but worth a try lol, think thats more a C/C++ thing

    I've never used BlueJ so i don't under stand its calling methods. I would also try new int[1][2] that would be the Java way. I do know someone else who is Studying Computing, I know he uses blueJ as i've helped him from time to time. I'll see if he has encountered this error.

    Quote Originally Posted by JavaPF View Post
    Good man Chris, that allowed me to compile it. I wonder if there is a way in Eclipse to select the entry point?!
    I Doubt it, Eclipse compiles the source and then runs it, BlueJ does alot of work behind the scene's to make it easier on beginners. Also it allows for quick method testing which is quite nice I have to admit.

    Regards,
    Chris

  16. The Following 2 Users Say Thank You to Freaky Chris For This Useful Post:

    bruint (May 1st, 2009), JavaPF (May 1st, 2009)

  17. #16
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Another <identifier> expected problem...

    Ahhhh Chris, you bloody legend. it was indeed {{3,2}}, now I just have to work out the rest of it Much thanks. I'll definately be back here if I can't work anything out.

  18. #17
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Another <identifier> expected problem...

    You are more than welcome, I'm glad I could help.

    If it's solved would you be as kind as to mark this as solved

    Thanks,
    Chris

  19. #18
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: Another <identifier> expected problem...

    Quote Originally Posted by bruint View Post
    Ahhhh Chris, you bloody legend. it was indeed {{3,2}}, now I just have to work out the rest of it Much thanks. I'll definately be back here if I can't work anything out.
    Super Chris to the rescue!

    If your question has been solved bruint, please mark your thread as solved (see my signature) Thanks!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  20. #19
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Another <identifier> expected problem...

    Ha!, already stumped myself.

    How can I get an integer from an Array and store it as a variable?

    This was my intention of:
      x = initialGrid.length;
                y = initialGrid[x-1].length;

    But if I use System.out.println(x); and System.out.println(y); they always return 1, and 2 respectively. Regardless of what numbers I input.

  21. #20
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Another <identifier> expected problem...

    x = initialGrid.length;
    y = initialGrid[x-1].length;
    This will store the length of the first dimension, ie number of slots it has into x and the length of the second demension along the (x-1)'th element in the first dimension into y.

    To store that value's of an array you do this.
    x = initialGrid[0][0]; // First Element in both dimensions

    If you would like me to draw a table up for you to make arrays clearer then let me know. I'm guessing you are new to Arrays, and I know they can be complicated to learn at first.

    Regards,
    Chris

  22. #21
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Another <identifier> expected problem...

    Drawing up a table isn't really required, I understand the concept of 2D Arrays (You are right I am new, haha) but I'm having trouble understand how I can get each of these numbers that I've input and store them as variables x and y. Because I want to be able to use them as co-ordinates for drawing squares (which you may have noticed earlier).
    So if I put in {{2,3}} I want x = 2 and y=3 so that I can say x*100 and y*100 to get my coordinates for SimpleCanvas, if that makes any sense?

    Ha, coding is such a tedious and frustrating thing. Im just hoping it gets marginally better with time.

    Thanks again Chris.

  23. #22
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Another <identifier> expected problem...

    {{2,3}}

    This is an Array that looks like this,
    [ 2 ] [ 3 ]

    the two is stored in initialGrid[0][0] and the 3 in initialGrid[0][1]

    so you would do this,
    x = initialGrid[0][0];
    y = initialGrid[0][1];

    Chris

  24. The Following User Says Thank You to Freaky Chris For This Useful Post:

    bruint (May 1st, 2009)

  25. #23
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Another <identifier> expected problem...

    Now I get it Thank you very very much.

  26. #24
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Another <identifier> expected problem...

    Your Welcome.

    Chris

Tags for this Thread