Hello,
I am getting Identifier expected and Illegal start of Expression erros in this code:

//"Math is our Game" - A math testing program for First through Fourth grade students
 
//Import GUI packages
 

import javax.swing.*;
import BreezySwing.*;
import java.awt.*; 
import java.util.Random; 
//import TurtleGraphics.*;
 
public class Math extends GBFrame
{
 
//declare variables for the window objects
 
private JLabel         greetingLabel;
private JLabel         additionLabel;
private JLabel         subtractionLabel;
private JLabel         multiplicationLabel;
private JLabel         divisionLabel;
private JLabel         randomLabel;
private JLabel         grade1Label;
private JLabel         grade2Label;
private JLabel         inputLabel;
private JLabel         gradeLabel;
private JLabel         questionLabel; 
 
private JTextField     greetingField;
private JTextField     additionField;
private JTextField     subtractionField;
private JTextField     multiplicationField;
private JTextField     divisionField;
private JTextField     randomField;
private JTextField     grade1Field;
private JTextField     grade2Field;
private JTextField     inputField;
private JTextField     questionField; 
 
private JButton         greetingButton;
private JButton         additionButton;
private JButton         subtractionButton;
private JButton         multiplicationButton;
private JButton         divisionButton;
private JButton         randomButton;    
private JButton         grade1Button;
private JButton         grade2Button;
private JButton         grade3Button;
private JButton         grade4Button;
private JButton         inputButton;
private JButton         quitButton;
private JButton         questionButton; 
/*int generator1=0;
int generator2=0;
int generator3=0;
int generator4=0;*/
 
JTextArea greetingArea;
 
Random generator = new Random(5);
//constructor
public Math()
{
   setUp();
}
 
//Open greeting screen with welcome and Start button
public void setUp()
{
greetingLabel = addLabel("Greeting", 1, 1, 1, 1);//greetingLabel.setText("Greeting"); //= addLabel      ("Greeting" ,1,1,1,1);
greetingArea = addTextArea("Welcome to Math is our Game, \na math skills program by \nNine Fingers and Ten Toes Software, Inc.", 2, 3, 3, 3);
greetingButton = addButton      ("Start" ,1,3,1,1);
}
 
//Button clicked to start game
 
public void buttonClicked(JButton buttonObj)
{
  this.setVisible(false);
  math3 test = new math3();
  test.setVisible(true);
  test.setSize(500, 500);
 
//First grade selected
 
//public void buttonClicked  (JButton button)
  //{
if (buttonObj == grade1Button)
{
JOptionPane.showMessageDialog(null, "Hi");
//Generate numbers for First Grade problem
 
     int generator1 = generator.nextInt(5);
     int generator2 = generator.nextInt(5);
 
//Select type of problem 
 
//instantiates and add window objects to the window
 
  
     }
 
      //public void buttonClicked  (JButton buttonObj)
 

     //{
 
//local variables
 
        Math2 math1 = new Math2();
 
//determine which button was clicked
 
         if (buttonObj == grade2Button)
 
      {
          
//setting and getting the numbers for the equations
 
        math1.setaddition(Double.parseDouble(additionField.getText()));
        //subtractionField.setNumber(math1.getsubtraction());
        //math1.setmultiplication(Double.parseDouble(multiplicationField.getText()));
        //divisionField.setNumber (math1.getdivision());
        //math1.setrandom(randomField.getText());
         }
}
 
class math3 extends GBFrame
{
 //Select grade level - Instantiate and add window objects to grade level window
JLabel gradeLabel = addLabel("Please select grade level of math problem.", 1, 1, 1, 1);// ,1,1,1,1);
JButton grade1Button = addButton  ("First Grade" ,1,1,1,1);
JButton grade2Button = addButton  ("Second Grade" ,2, 1,1,1);
JButton grade3Button = addButton  ("Third Grade" ,3,1,1,1);
JButton grade4Button = addButton  ("Fourth Grade" ,4,1,1,1);
JButton quitButton = addButton  ("Quit" ,5,1,1,1); 
JButton gobackButton = addButton ("Revert Back" ,6,1,1,1);
 
public void buttonClicked(JButton buttonObj)
{
 
if (buttonObj == quitButton)
System.exit(0);
 
if (buttonObj == gobackButton)
{
    //Select grade level - Instantiate and add window objects to grade level window
 
    math3 s = new math3();
    
    gobackButton = addButton ("Revert Back" ,6,1,1,1);
      
}
 

if (buttonObj == grade1Button)
{

addButtons P = new addButtons();
 
this.setVisible(false);
P.setVisible(true);
P.setSize(700, 700);
 
}
}
}
  public static void main(String [] args)
  {
   Frame frm = new Math(); 
   frm.setSize(500,500);
   frm.setVisible(true);
  
  }

class Math2
 
{
 
private double Mathsubtraction;
 
public void setaddition(double math)
 
{
 
Mathsubtraction = math;
 
}
 
public double setsubtraction(double math)
 
{
 
return Mathsubtraction;
 
}
 
public double getsubtraction()
 
{
 
return Mathsubtraction;
 
}
 
public double getaddition()
 
{
        double Mathaddition = 0;
 
return Mathaddition;
 
}
 
    String getdivision() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
 
    void setdivision(double parseDouble) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
 
    void setmultiplication(double parseDouble) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
 
    void setrandom(double parseDouble) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    void getrandom() {
        throw new UnsupportedOperationException("Not yet implemented");
}
 
      } 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
 
/**
 *
 * @author Tyler
 */
class math2 {
 
    void setaddition(double parseDouble) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
 
}
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author TBoden
 */
class addButtons extends GBFrame
{
     JLabel additionLabel = addLabel         ("Addition" ,1,2,1,1);
      JLabel subtractionLabel = addLabel      ("Subtraction" ,1,3,1,1);
      JLabel multiplicationLabel = addLabel   ("Multiplication" ,1,4,1,1);
      JLabel divisionLabel = addLabel         ("Division" ,1,5,1,1);
      JLabel randomLabel = addLabel      ("Random" ,1,6,1,1);
      JLabel gobackLabel = addLabel      ("Revert Back" ,1,7,1,1);
      
      JTextField additionField = new JTextField("0");
      JTextField subtractionField = new JTextField("0");
      JTextField multiplicationField = new JTextField("0");
      JTextField divisionField = new JTextField("0");
      JTextField randomField = new JTextField("0");
      JTextField gobackField = new JTextField("0");
      
      JButton additionButton = addButton         ("1+1" ,1,5/3,1,1);
      JButton subtractionButton = addButton      ("1-1" ,4,1,1,1);
      JButton multiplicationButton = addButton   ("1*1" ,5,1,1,1);
      JButton divisionButton = addButton       ("1/1" ,6,1,1,1);
      JButton randomButton = addButton        ("Random Equation"  ,7,1,1,1);
      JButton gobackButton = addButton        ("Revert Back" ,8,1,1,1);
   
      public void buttonClicked(JButton buttonObj)
        {
 
      if (buttonObj == additionButton)
      {
          Math4 m = new Math4();
      
      this.setVisible(false);
      m.setVisible(true);
      m.setSize(700, 700); 
      }
      if (buttonObj == subtractionButton)
      {
      
      Math5 m = new Math5();
      
      this.setVisible(false);
      m.setVisible(true);
      m.setSize(700, 700);
      }
      
if (buttonObj == multiplicationButton)
{
      
      Math6 m = new Math6();
      
      this.setVisible(false);
      m.setVisible(true);
      m.setSize(700, 700);    
}
 
 if (buttonObj == divisionButton)
      {
      
      Math12 m = new Math12();
      
      this.setVisible(false);
      m.setVisible(true);
      m.setSize(700, 700);
          
    }
      
       if (buttonObj == randomButton)
      {
      
      Math7 m = new Math7();
      
      this.setVisible(false);
      m.setVisible(true);
      m.setSize(700, 700);
          
    }
       
      } 
      
      
 class Math12 extends GBFrame{
   Random generator = new Random();
    
    private JButton SubmitButton = addButton ("Submit Answer" ,1,9/8,1,1);
    int generator1 = generator.nextInt(20)+1/2;
    int generator2 = generator.nextInt(20)+1/2;
    int answer = generator1/generator2;
    
    JTextField answerField = addTextField("" ,3/2,2,1,1);
    
    JLabel questionLabel = addLabel(generator1 + "/" + generator2 + "=" ,1,30/23,20,20);
  
  }
 
 class Math4 extends GBFrame{
 
 public void additionClicked()
 {
     Random generator = new Random();
    
    
    private JButton SubmitButton = addButton ("Submit Answer" ,1,9/8,1,1);
    int generator1 = generator.nextInt(9)+1;
    int generator2 = generator.nextInt(9)+1; 
    int answer = generator1+generator2;
    
    JTextField answerField =  addTextField("" ,3/2,2,1,1);
    
    JLabel questionLabel =  addLabel(generator1 + "+" + generator2 +"=" ,1,30/23,20,20);
    
    if (answer == 1)
    {
   
    }
     }
 
 
 }

 
 class Math6 extends GBFrame{
    Random generator = new Random();
    
    private JButton SubmitButton = addButton ("Submit Answer" ,1,9/8,1,1);
    int generator1 = generator.nextInt(5)+1;
    int generator2 = generator.nextInt(5)+1;
    int answer = generator1*generator2;
    
    JTextField answerField = addTextField("" ,3/2,2,1,1);
    
    JLabel questionLabel = addLabel(generator1 + "*" + generator2 + "=" ,1,30/23,20,20);
      
  }
 
 class Math5 extends GBFrame{
    
    Random generator = new Random();
    
    private JButton SubmitButton = addButton ("Submit Answer" ,1,9/8,1,1);
    int generator1 = generator.nextInt(9)+1;
    int generator2 = generator.nextInt(9)+1;
    int answer = generator1-generator2;
    
    JTextField answerField = addTextField("" ,3/2,2,1,1);
    
    JLabel questionLabel = addLabel(generator1 + "-" + generator2 + "=" ,1,30/23,20,20);
    
}
 class Math7 extends GBFrame{
      
    Random generator = new Random();
    
    private JButton SubmitButton = addButton ("Submit Answer" ,1,9/8,1,1);
    int generator1 = generator.nextInt(9)+1;
    int generator2 = generator.nextInt(9)+1;
    int counter = generator.nextInt(4)+1;
   
    
    int answer = generator1+generator2;
    JLabel questionLabel = addLabel(generator1 + "+" + generator2 + "=" ,1,30/23,20,20);
    
    
    
    JTextField answerField = addTextField("" ,3/2,2,1,1);
    
    
    
 }

}

the errorers are at:
class Math4 extends GBFrame{

public void additionClicked()
{
Random generator = new Random();


private JButton SubmitButton = addButton ("Submit Answer" ,1,9/8,1,1);
int generator1 = generator.nextInt(9)+1;
int generator2 = generator.nextInt(9)+1;
int answer = generator1+generator2;

JTextField answerField = addTextField("" ,3/2,2,1,1);

JLabel questionLabel = addLabel(generator1 + "+" + generator2 +"=" ,1,30/23,20,20);

if (answer == 1)
{

}
}


Note: I know the code is not formatted well, with spaces and tabs. Sorry, it is no my code it is a student who I am helping.

Recommended Answers

All 20 Replies

If you expect a solution, please post the exact error code you are getting. It usually includes line numbers and method names, etc.

Note: I know the code is not formatted well, with spaces and tabs. Sorry, it is no my code it is a student who I am helping.

Use an advanced IDE like Eclipse or Netbeans, they have features like autoformat using which you can automatically format any code, no matter how badly it is written.

And just as BestJewSinceJC said, paste the exact error (without editing it) the compiler is throwing up.

The helper himself needs help ? Better tell the student to look at other places for solution I guess that way he would reach the solution faster. Why follow a bigger loop of he asking you and you asking us, that way he won't be able to understand how one got to the solution ain't it true ?

I am tutoring him, and ya sometimes I need help. And I will make sure he understnds the solution.

I am tutoring him, and ya sometimes I need help. And I will make sure he understnds the solution.

But how about you understanding us and pasting the exact compiler error and helping us help you help him ??? :P

And heres the errors:
File: F:\Math.java [line: 384]
Error: F:\Math.java:384: illegal start of type
File: F:\Math.java [line: 384]
Error: F:\Math.java:384: <identifier> expected
File: F:\Math.java [line: 384]
Error: F:\Math.java:384: ';' expected

Sorry it took me longer to post the errors, I didnt see that someone else had replied.

private JButton SubmitButton = addButton ("Submit Answer" ,1,9/8,1,1);

I think this is the problem, I do not see any addButton() function in your class Math4, which returns an object of type JButton.

I suggest both you and your student to go through this tutorial to get your concepts on creating GUI with swing right.

> int generator1 = generator.nextInt(20)+1/2;

Operator precedence anyone?

> /*
> * To change this template, choose Tools | Templates
> * and open the template in the editor.
> */

I am surprised how someone can churn out such an unformatted code even after using an IDE / advanced text editor.

> class Math12 { /* */ }
> class Math5 { /* */ }
> class Math7 { /* */ }

The very fact that multiple classes are created which do the same thing is a clear indication of design problems with the application. Consider customizing the class instance by having constructors/factory methods.

...and many other things.

If your friend really wants to learn, tell him to start some serious reading. A lot of beginner resources are mentioned in the sticky; starting with the Java beginner tutorials ain't that bad an idea.

commented: You must have really put in some effort to read all that <Ugh> code +5

I didnt see that someone else had replied

Almost everyone here tries to help and even verruckt would have if you would have given us the required details first and eventhough you would not like to hear it, verruckt is correct, although I do not mind the question on Java, you have not taught him to code properly. The first thing you should have taught him to do is get him to organize his code correctly, with proper indentations, line breaks etc. In case you too are unaware of any standard coding conventions, here is a link to Sun's Java coding conventions, you can either browse it online or download the PDF.

<EDIT>
Nice catch ~s.o.s~, that comment got by me

I've tried to copying the code you provided in Eclipse, to check for the linenumbers, but either you did not give us your most recent code, or you did not post all the exceptions you're getting.

you also should know, we don't have all the classes you do, so that 'll give us extra errors as well.

start with filtering out the errors the ide gives you before compilation before you try to compile it, than work on the bigger issues.

if your friend wants to learn to code properly, it might be better if he started off with the basics

If your friend really wants to learn, tell him to start some serious reading. A lot of beginner resources are mentioned in the sticky; starting with the Java beginner tutorials ain't that bad an idea.

@christiangirl
Thats what my point was when I said tell your friend to look at other places. But Alas !!!

Also talking about the code it isn't arranged in the best of the manners, quite the opposite infact, which even though is written by him does not seem to be adviced to be changed, by you. Also not only Math4 but none of your classes have an addButton() method and you have used it in all of them, which makes me conclude that you yourself might be not knowing about these things. Then what's the point in tutoring about something that you yourself are in the learning phase of. Wouldn't you be teaching something incorrect to him, may be even accidentally.

My next point was about delaying the entire process of getting to the solution by not directing him towards resources directly. Didn't you prove my point with your next post ?

Sorry it took me longer to post the errors, I didnt see that someone else had replied.

Ok, let me explain: I started tutoring him last thursday, while he was in middle of this program. That is why the code is formatted badly, the comments arent good, and other various problems you have all mentioned. If we had started from the beginning, yes things would be different.
And, addButton() worked before we added the code I mentioned I was having errors with. Although I am still learning myself, he is required to use BreezySwing and I have not uded BreezySwing in a couple of years, so I am having to review it, and if I am doing something wrong with it I will try to learn the right way.
And ya your right, I didnt post all of the errors, I realized late last night that I should have.
Here they all are:

File: F:\Math.java [line: 384]
Error: F:\Math.java:384: illegal start of type
File: F:\Math.java [line: 384]
Error: F:\Math.java:384: <identifier> expected
File: F:\Math.java [line: 384]
Error: F:\Math.java:384: ';' expected
File: F:\Math.java [line: 384]
Error: F:\Math.java:384: illegal start of type
File: F:\Math.java [line: 384]
Error: F:\Math.java:384: ';' expected
File: F:\Math.java [line: 461]
Error: F:\Math.java:461: illegal start of type
File: F:\Math.java [line: 461]
Error: F:\Math.java:461: <identifier> expected
File: F:\Math.java [line: 461]
Error: F:\Math.java:461: ';' expected
File: F:\Math.java [line: 461]
Error: F:\Math.java:461: illegal start of type
File: F:\Math.java [line: 461]
Error: F:\Math.java:461: <identifier> expected
File: F:\Math.java [line: 461]
Error: F:\Math.java:461: ';' expected
File: F:\Math.java [line: 463]
Error: F:\Math.java:463: <identifier> expected
File: F:\Math.java [line: 468]
Error: F:\Math.java:468: illegal start of type
File: F:\Math.java [line: 468]
Error: F:\Math.java:468: <identifier> expected
File: F:\Math.java [line: 468]
Error: F:\Math.java:468: ';' expected
File: F:\Math.java [line: 468]
Error: F:\Math.java:468: illegal start of type
File: F:\Math.java [line: 468]
Error: F:\Math.java:468: <identifier> expected
File: F:\Math.java [line: 468]
Error: F:\Math.java:468: ';' expected
File: F:\Math.java [line: 470]
Error: F:\Math.java:470: <identifier> expected
File: F:\Math.java [line: 475]
Error: F:\Math.java:475: class, interface, or enum expected
File: F:\Math.java [line: 479]
Error: F:\Math.java:479: class, interface, or enum expected
File: F:\Math.java [line: 481]
Error: F:\Math.java:481: class, interface, or enum expected
File: F:\Math.java [line: 486]
Error: F:\Math.java:486: class, interface, or enum expected
File: F:\Math.java [line: 490]
Error: F:\Math.java:490: class, interface, or enum expected
File: F:\Math.java [line: 498]
Error: F:\Math.java:498: class, interface, or enum expected
Thanks for all of your comments and help!

Somewhere you have a missing brace, paren, or semi-colon (or perhaps more than one). These should be rather simple structural syntax errors for you to locate. Just start looking backwards from the first line number the stack traces are reporting.

Formatting the code better would also help spot such structural problems, by the way.

Ya, I was looking for a missing or extra one yesterday but couldnt find it. I guess I'll keep looking. Or I'm hopping he found it today at school.

Thanks everyone! All of your comments actually did help me!

Hey, have gone through the whole code looking for what could be causing the problem and have found nothing.

Nevermind, I figured it out. Thanks for all of your help!

So what was the problem in the code exactly?

seriously, Rodrigo, you think she's going to come back after four years to explain?
no reason at all to revive this thread.

commented: agreed +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.