954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need help with a answer checker

below is my current code for a math game i'm having problems creating the answer checking part of it, any assistance is appreciated.

import javax.swing.*;
import java.util.Random;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.event.*;

import javax.swing.*;


public class MathPanel extends JPanel {
// Declares  Labels and Buttons
	public JButton AddButt , SubButt , MultiButt , DivideButt , Submit ;
	public JTextField Answer ;
	public JLabel Select , Result , Counter ,ImgLbl, Question  ;
	int CountR , CountW; 

	
	
	
	public MathPanel(){
// Sets Up the Gui interface
		ImageIcon icon = new ImageIcon ("blank.png");
		ImageIcon icon1 = new ImageIcon ("sad.png");
		ImageIcon icon2 = new ImageIcon ("happy.png");
		setPreferredSize (new Dimension(300, 400));
	    setBackground (Color.WHITE);
	   
	    
		Select = new JLabel ("Choose Your Question Type:");
		Question= new JLabel ("Question Is Yet To Be Generated");
		Result = new JLabel ("Submit Your Answer When You Are Ready");
		ImgLbl = new JLabel (icon);
		Counter= new JLabel ("Right:" + CountR + " & Wrong :" + CountW);
		AddButt = new JButton ("Add");
		SubButt = new JButton ("Subtract");
		MultiButt = new JButton ("Multiply");
		DivideButt = new JButton ("Divide");
		Submit = new JButton ("Submit");
		//creates listeners to spawn questions when buttons are clicked
		AddButt.addActionListener(new TempListener());
		SubButt.addActionListener(new TempListener());
		MultiButt.addActionListener(new TempListener());
		DivideButt.addActionListener(new TempListener());
		Answer = new JTextField(10);
		Answer.addActionListener(new TempListener());
		Submit.addActionListener(new AnswerListener());
		//puts the buttons and labels on the gui
		
		add (Select);
		add (AddButt);
		add (SubButt);
		add (MultiButt);
		add (DivideButt);
		add (Question);
		add (Answer);
		add (Submit);
		add (Counter);
		add (Result);
		add (ImgLbl);
	}
		private class TempListener implements ActionListener{

			int R1 , R2 ;
				Random generator= new Random();
				
				public void actionPerformed(ActionEvent Event) {
			if (Event.getSource()==(AddButt)){
				R1 = generator.nextInt(10)+1;
				R2 = generator.nextInt(10)+1;
				Question.setText(R1 + "+" + R2) ;
			}
			
			 if (Event.getSource()==(SubButt)){
				R1 = generator.nextInt(10)+1;
				R2 = generator.nextInt(10)+1;
				Question.setText(R1 + "-" + R2) ;
			 }
			if (Event.getSource()==(MultiButt)){
					R1 = generator.nextInt(10)+1;
					R2 = generator.nextInt(10)+1;
					Question.setText(R1 + "*" + R2) ;	
						}
			if (Event.getSource()==(DivideButt)){
				R1 = generator.nextInt(10)+1;
				R2 = generator.nextInt(10)+1;
				Question.setText(R1 + "/" + R2) ;
						
						}
je5ter461
Newbie Poster
3 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

At the four places where you construct the question you can also calculate the correct answer, and save it in a variable so you can check the user's answer against that value when they press "submit".

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
At the four places where you construct the question you can also calculate the correct answer, and save it in a variable so you can check the user's answer against that value when they press "submit".

ya my problem is when im trying to get the text from the textfield and compare it to my saved integer answer

je5ter461
Newbie Poster
3 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

You need to parse the text from the textfield to get its integer value, Have a look at the Integer.parseInt method.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
if (Event.getSource()==Submit)   {
				
			String text = Answer.getText();
			Temp=Integer.parseInt(text);
				if (Temp.equals(Ans)){
				Result.setText("You are Correct");
				
				ImgLbl = new JLabel (icon2);
				CountR++ ;
				}
				else
					 Result.setText("You are Wrong");
					CountW++ ;
					ImgLbl = new JLabel (icon1);

When I hit my submit button it doesnt check anything nor does my counter update

je5ter461
Newbie Poster
3 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: