Question:
help java code!!!! IT compiles but doesnt campare answers. SUBMIT button has problem?
neo ☆
2008-10-28 21:33:53 UTC
main class plz check its answerCorrect() method
here is the code
public class Quiz
{

private String question, answer;
private boolean feildAnswer;
public Quiz(String question)
{
this.question = question;
}


public Quiz(String question, String answer)
{
this.question = question;
this.answer= answer;
}

public String getQuestion()
{
return question;
}

public String getAnswer()
{
return answer;
}

//check it not working
public boolean answerCorrect(String userAnswer)
{
if(userAnswer == answer)
return true;

else return false;


}

public String toString()
{
return "QUESTION IS: " + getQuestion() + "ANSWER IS: " + getAnswer();
}

}

here is second class look at actionPerformed method

SUBMIT button is not working logicaly doesnot compare to answers...
CODE IS:-


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.lang.*;

// Submit method and parse String method
//

public class QuestionTest extends JPanel implements ActionListener
{
private JLabel label1, label2, label3;
private JTextField feild1 = null;
private JButton HISTORY, GEOGRAPHY, ASTRONOMY, SUBMIT;
private Quiz HQuestion, AQuestion, GQuestion;
private Quiz HAnswer, AAnswer, GAnswer;
String CurrentQues = null;
Quiz CurrentAns = null;
public QuestionTest()
{
HQuestion = new Quiz(" Who was first owner of Kohinoor? "," Mahararaja Ranjit Singh. ");
AQuestion = new Quiz(" What is ISS? "," International Space Station. ");
GQuestion = new Quiz(" Where is nelam valey? "," Jammu & Kashmir. " );
label1 = new JLabel("Slect a Button to answer the question: ");
label2 = new JLabel("Enter your answer click SUBMIT");
label3 = new JLabel(" ");
feild1 = new JTextField(20);
HISTORY = new JButton("HISTORY");
GEOGRAPHY = new JButton("GEOGRPHY");
ASTRONOMY = new JButton("ASTRONOMY");
SUBMIT = new JButton("SUBMIT");
HISTORY.addActionListener(this);
GEOGRAPHY.addActionListener(this);
ASTRONOMY.addActionListener(this);
SUBMIT.addActionListener(this);
// add the content to this panel
add(label1);
add(HISTORY);
add(GEOGRAPHY);
add(ASTRONOMY);
add(feild1);
add(label2);
add(SUBMIT);
add(label3);
}

public static void main(String[] args)
{
QuestionTest panel = new QuestionTest();
JFrame frame = new JFrame("Question and answer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setLocationRelativeTo(null); //position frame at center
frame.pack(); // resize the frame appropriately for its content
frame.setSize(200,400);
frame.setVisible(true);
}

public String historyQuestion()
{
return HQuestion.getQuestion();
}

public String geographyQuestion()
{
return GQuestion.getQuestion();
}

public String astronomyQuestion()
{
return AQuestion.getQuestion();
}

public Quiz historyAnswer()
{
return HQuestion;
}
public Quiz geographyAnswer()
{
return GQuestion;
}
public Quiz astronomyAnswer()
{
return AQuestion;
}

public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == HISTORY)
{
CurrentAns = historyAnswer();
CurrentQues = historyQuestion();
label1.setText(historyQuestion());
}

else if(source == GEOGRAPHY)
{
CurrentAns = geographyAnswer();
CurrentQues = geographyQuestion();
label1.setText(geographyQuestion());
}

else if(source == ASTRONOMY)
{
CurrentAns = astronomyAnswer();
CurrentQues = astronomyQuestion();
label1.setText(astronomyQuestion());
}

else if((source == SUBMIT) && (feild1.getText() != null))
{
if(CurrentAns.answerCorrect(feild1.getText()))
{
label2.setText("Wrong answer");
repaint();
}
else
{
label2.setText("Correct answer");
repaint();
}
}
}
}
Three answers:
sspade30
2008-10-28 21:46:50 UTC
== doesn't do a string comparison.



Use .equals:



if (userString.equals(otherstring))
?
2016-05-23 12:39:35 UTC
Oh yes, I have had a deliciously funny feeling sometimes, and sometimes I don't dare click the Submit button and have to cancel it.
ikeman32
2008-10-28 22:06:17 UTC
Java is not my forte but it looks as if this line is your problem child



else if((source == SUBMIT) && (feild1.getText() != null))

{

if(CurrentAns.answerCorrect(feild1.getTe...

{ <----shouldn't this be a closing brace?





or see below



else if((source == SUBMIT) && (feild1.getText() != null))

{

if(CurrentAns.answerCorrect(feild1.getTe...

{

label2.setText("Wrong answer");

repaint();

}

else

{

label2.setText("Correct answer");

repaint();

}

}

}

} <----Do you perhaps have one too many closing braces.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...