| | |
Identifier expected and Illegal start of Expression errors
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 108
Reputation:
Solved Threads: 1
Hello,
I am getting Identifier expected and Illegal start of Expression erros in this code:
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.
I am getting Identifier expected and Illegal start of Expression erros in this code:
Java Syntax (Toggle Plain Text)
//"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.
•
•
•
•
Originally Posted by christiangirl
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.
And just as BestJewSinceJC said, paste the exact error (without editing it) the compiler is throwing up.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
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 ?
Last edited by verruckt24; Jan 14th, 2009 at 3:04 am.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Last edited by stephen84s; Jan 14th, 2009 at 3:56 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
java Syntax (Toggle Plain Text)
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
> 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.
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.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Please help Errors will not compile (Java)
- Case gives 100 errors, please help (Java)
- Inventory Program Part5 Problems (Java)
- I Hope Someone Who Is Online Will Help......Java Code (Java)
- Help on some errors (Java)
Other Threads in the Java Forum
- Previous Thread: network monitoring
- Next Thread: Sorting Bars
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character chat class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list loops mac map method methods mobile netbeans newbie notdisplaying number online printf problem program programming project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






