-
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)
Code :
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(){
//}
}
-
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:
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:
Code :
/**
* 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);
}
}
}
-
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
-
Re: Another <identifier> expected problem...
Thanks Chris, I've posted the SimpleCanvas code as requested. :)
-
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
-
Re: Another <identifier> expected problem...
Thanks very much. Any help is greatly appreciated:) Hope to hear from you soon.
-
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.
-
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?
-
Re: Another <identifier> expected problem...
Hi, I do not understand your problem exactly, but I think this may be the problem.
Code :
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,
Code :
x = initialGrid.length;
y = initialGrid[x-1].length;
It then runs fine, well as far as i can tell :P
Regards,
Chris
-
Re: Another <identifier> expected problem...
Quote:
Originally Posted by
JavaPF
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,
Code :
public static void main(String[] args){
new FifteenPuzzle(new int[1][2]);
}
Regards,
Chris
-
Re: Another <identifier> expected problem...
Quote:
Originally Posted by
Freaky Chris
BlueJ allows you to specify the program entry point. Thus no main function is required.
Add this to the
FifteenPuzzle class,
Code :
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?!
-
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:
Code :
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:
http://img56.imageshack.us/img56/7935/picture2sii.png
-
Re: Another <identifier> expected problem...
Hmm... I don't get this error? Does it appear when you compile the code?
-
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?
-
Re: Another <identifier> expected problem...
bruint, you do not require
Code :
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 :P
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
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
-
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:D Much thanks. I'll definately be back here if I can't work anything out.
-
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
-
Re: Another <identifier> expected problem...
Quote:
Originally Posted by
bruint
Ahhhh Chris, you bloody legend. it was indeed {{3,2}}, now I just have to work out the rest of it:D 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! :)
-
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:
Code :
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.
-
Re: Another <identifier> expected problem...
Code :
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.
Code :
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
-
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.
-
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,
Code :
x = initialGrid[0][0];
y = initialGrid[0][1];
Chris
-
Re: Another <identifier> expected problem...
Now I get it:) Thank you very very much.
-
Re: Another <identifier> expected problem...