Compilation error: illegal start of expression

I am learning Java (The Gaming Side). I bought a book and it has a code in which I tried to copy and test it. The only problem is that it occurs with errors when trying to compile it.

C:\Users\James\Desktop\Java>Javac GamePanel.java GamePanel.java:57: illegal start of expression private void gameUpdate() ^ GamePanel.java:57: illegal start of expression private void gameUpdate() ^ GamePanel.java:57: ';' expected private void gameUpdate() ^ GamePanel.java:64: reached end of file while parsing }โ†’ ^ 4 errors 

Code:

 public class GamePanel extends JPanel implements Runnable { private static final int PWIDTH = 500; private static final int PHEIGHT = 400; private Thread animator; private volatile boolean running = false; private volatile boolean gameOver = false; public GamePanel() { setBackground(Color.white); setPreferredSize( newDimension(PWIDTH, PHEIGHT)); } public void addNotify() { super.addNotify(); startGame(); } public void startGame() { if (animator == null || !running) { animator = new Thread(this); animator.start(); } } public void stopGame() { running = false; } public void run() { running = true; while(running) { gameUpdate(); gameRender(); repaint(); try { Thread.sleep(20); } catch(InterruptedException ex) { } System.exit(0); } private void gameUpdate() { if (gameOver == false) { } } } 

I know that I'm probably doing something wrong, but I checked it again and again, can someone please enlighten me on what I am doing wrong?

+4
source share
9 answers

Your missing } while loop is not closed.

 public void run() { running = true; while(running) { gameUpdate(); gameRender(); repaint(); try { Thread.sleep(20); } catch(InterruptedException ex) { } } // <<< this is the missing brace System.exit(0); } 

You might want to get an IDE like eclipse , netbeans, or intellij (all for free) and use them to format your code ... things like missing curly braces get a lot easier to find when your code is formatted correctly.

+5
source

You missed closing} for the while loop. And if you're interested (and you should be), he says "illegally starting the expression because" private ... "is trying to start a new block of code, but the Java parser knows that it hasn't completed the block yet.

+1
source

Check your run() method. You are missing the closing brace to complete the while loop. The declaration of the gameUpdate () method is then included in the previous function (incorrectly)

+1
source

You are missing the parenthesis ending the While loop.

+1
source

You have not closed the loop } .

I suggest you use an IDE that points this out for you.

http://netbeans.org

+1
source

You donโ€™t have enough closing} before System.exit (0); in run ()

+1
source

Two missing end brackets! Try using an editor that highlights such omissions. Eclipse is big and bulky, but really helps you when you go through this learning curve. Notepad ++ is lean and average, but you have to check the corresponding brackets yourself (it will find them for you).

+1
source

you miss the closing brackets for the While loop in the start method ... Design using IDEs like Netbeans, Eclipse, etc. to avoid missing braces because they are automatically inserted when necessary. hope he solves ur problem

0
source

you do not have enough half-columns, try: private void gameUpdate ();

0
source

All Articles