Results 1 to 10 of 10
 10312011, 08:19 AM #1Member
 Join Date
 Oct 2011
 Posts
 10
 Rep Power
 0
Java Arithmetic Program. PLEASE HELP!
I can't figure out how in the world to increase total, and correct if the user correct or incorrectly answers the randomly generated basic arithmetic problem. I would GREATLY appreciate any help. I know my code is wrong, but can't find anything on how to fix it :// ahh frustration. lol. THANK YOU!
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Random; public class MathPanel extends JPanel { private JButton addition, subtraction, mult, divison, submit; private JTextField math; private JLabel welcome, question, total, correct; private String imageFileName = "sad.gif"; private ImageIcon icon = new ImageIcon(imageFileName); //  // Constructor: Sets up the main GUI components. //  public MathPanel() { welcome = new JLabel("Choose the question type:"); question = new JLabel("Question is yet to be generated."); addition = new JButton("Addition +:"); subtraction = new JButton("Subtraction "); mult = new JButton("Multiplication *"); divison = new JButton("Divison /"); submit = new JButton("Submit"); correct = new JLabel("Correct:"); total = new JLabel("Total:"); math = new JTextField(35); math.addActionListener(new TempListener()); addition.addActionListener(new TempListener()); subtraction.addActionListener(new TempListener()); mult.addActionListener(new TempListener()); divison.addActionListener(new TempListener()); add(welcome); add(addition); add(subtraction); add(mult); add(divison); add(question); add(math); add(submit); add(correct); add(total); setPreferredSize(new Dimension(400, 400)); setBackground(Color.white); } // ***************************************************************** // Represents an action listener. // ***************************************************************** private class TempListener implements ActionListener { //  // Sets up the Question for the user to answer. //  public void actionPerformed(ActionEvent event) { if (event.getSource() == addition) { Random addRand = new Random(); int a = addRand.nextInt(10) + 1; int a1 = addRand.nextInt(10) + 1; question.setText(a + " + " + a1); } if (event.getSource() == subtraction) { Random subRand = new Random(); int s = subRand.nextInt(10) + 1; int s1 = subRand.nextInt(10) + 1; question.setText(s + "  " + s1); } if (event.getSource() == mult) { Random multRand = new Random(); int x = multRand.nextInt(10) + 1; int x1 = multRand.nextInt(10) + 1; question.setText(x + " * " + x1); } if (event.getSource() == divison) { Random multRand = new Random(); int d = multRand.nextInt(100) + 1; int d1 = multRand.nextInt(10) + 1; question.setText(d + " / " + d1); } if (event.getSource() == submit){ { int mathTemp, qTemp; String text = math.getText(); String text2 = question.getText(); mathTemp = Integer.parseInt (text); qTemp = Integer.parseInt (text2); if (mathTemp == qTemp) { correct = new JLabel("Correct:" + 1); total = new JLabel ("Total:" + 1); } else { correct = new JLabel("Correct " + 0); total = new JLabel ("Total:" + 1); } } } } } }
Last edited by JosAH; 10312011 at 08:22 AM. Reason: added [code] ... [/code] tags
 10312011, 09:09 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,824
 Blog Entries
 7
 Rep Power
 21
Re: Java Arithmetic Program. PLEASE HELP!
Don't just define JLabels named 'total' and 'correct' but also keep track of two ints 'totalQuestion' and 'correctQuestion'. Adjust those ints after the user has answered a question and adjust the text in those JLabels accordingly.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 10312011, 09:20 AM #3Member
 Join Date
 Oct 2011
 Posts
 10
 Rep Power
 0
Re: Java Arithmetic Program. PLEASE HELP!
But I can't have an int within the JLabel parameter though can I? I have to have Total: and Correct: there even when the user has yet to answer a question, and the fields have nothing. When I use correctQ and totalQ and put them as the parameters of my JLabel then increase correctQ and totalQ under the Submit part, it says that I can't have an int parameter for a JLabel
 10312011, 09:24 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,824
 Blog Entries
 7
 Rep Power
 21
Re: Java Arithmetic Program. PLEASE HELP!
Something like this, mayhap?
Java Code:// updating the JLabels: correct.setText("Correct: "+correctQ); total.setText("Total: "+totalQ);
Joscenosillicaphobia: the fear for an empty beer glass
 10312011, 09:29 AM #5Member
 Join Date
 Oct 2011
 Posts
 10
 Rep Power
 0
Re: Java Arithmetic Program. PLEASE HELP!
Well I tried that, and it compiled fine, but still when I enter my answer, nothing is updated :(, whether my answer is right or wrong, it doesn't add anything to correct or total. I really don't think this portion of my code is working right,
Java Code:if (event.getSource() == submit){ { int mathTemp, qTemp; String text = math.getText(); String text2 = question.getText(); mathTemp = Integer.parseInt (text); qTemp = Integer.parseInt (text2); if (mathTemp == qTemp) { totalQ = totalQ + 1; correctQ = correctQ + 1; correct.setText("Correct: "+correctQ); total.setText("Total: "+totalQ); } else { totalQ = totalQ + 1; total = new JLabel ("Total:" + totalQ);
Last edited by Ahdood; 10312011 at 09:32 AM. Reason: Forgot some code
 10312011, 09:36 AM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,824
 Blog Entries
 7
 Rep Power
 21
Re: Java Arithmetic Program. PLEASE HELP!
Did I create new JLabels in my code snippet?
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 10312011, 09:39 AM #7Member
 Join Date
 Oct 2011
 Posts
 10
 Rep Power
 0
Re: Java Arithmetic Program. PLEASE HELP!
No, I took mine out as well as you can see in the snippet I posted, but it still didn't work after compiling. I don't know where exactly it's wrong, it looks good :/
 10312011, 09:52 AM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,824
 Blog Entries
 7
 Rep Power
 21
Re: Java Arithmetic Program. PLEASE HELP!
Use your creativity and imagination: System.out.println( ... ) the new text you're trying to set in your JLabels and see if your code at least attempts to do the correct thing. It doesn't help you much if you only do what was suggested here, come back if it doesn't work as you expected and wait for new suggestions ...
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 10312011, 10:02 AM #9Member
 Join Date
 Oct 2011
 Posts
 10
 Rep Power
 0
Re: Java Arithmetic Program. PLEASE HELP!
It's not a System.out.println, I need my total and correct to show up in the application's frame, not in the eclipse console, I honestly don't know enough about GUI things within Java to creatively guess and have it work, I need some pointers, I understand that I should attempt it on my own, but i'm afraid i've exhausted both my creativity and knowledge. Any help would be greatly appreciated.
 10312011, 10:15 AM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,824
 Blog Entries
 7
 Rep Power
 21
Re: Java Arithmetic Program. PLEASE HELP!
I didn't write that: use System.out.println( ... ) to see if your code at least attempts to set those new JLabel values; you can always remove those System.out.println( ... ) later but in the mean time you can actually see what your program is doing so you don't have to guess and you don't have to come here after every little step that didn't work as you expected. Programming also is about taking initiative. It is not about throwing your hands up in the air and let others solve your issue(s) and complaining.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
Similar Threads

Java Thread arithmetic value
By mike_ledis in forum Threads and SynchronizationReplies: 0Last Post: 03152011, 07:28 AM 
Arithmetic using doubles
By Black_Eye in forum New To JavaReplies: 4Last Post: 10192010, 08:35 AM 
Abbreviated Arithmetic
By SweetLD215 in forum New To JavaReplies: 19Last Post: 10132010, 12:46 AM 
Polynomial Arithmetic Java
By thisisIT in forum New To JavaReplies: 3Last Post: 03092010, 06:33 PM 
Arithmetic Stacks
By unc123w in forum New To JavaReplies: 22Last Post: 10212008, 08:24 PM
Bookmarks