hey guys/...am stuck at one point....help me please! ! !

its a quiz progg... one ques...4 options...

i have one button---NEXT QUES--- It should display the next ques--it should also take the user selected option, compare it with the correct option and show if the Answer is right or wrong

else if(ae.getSource()==b1)
{

if(answer==Answercheck.correct) // checking if answer is correct or not
{
System.out.println("YOUR ANSWER IS CORRECT");
}
else
{
System.out.println("YOUR ANSWER IS WRONG");
}



if(option1.isSelected())
{
System.out.println("Option 1");
answer = option1.getText();
System.out.println(" User Selected ANSWER IS : : : :  "+answer);
}
else if(option2.isSelected())
{
System.out.println("Option 2");
answer = option2.getText();
System.out.println(" User Selected ANSWER IS : : : :  "+answer);
}
else if(option3.isSelected())
{
System.out.println("Option 3");
answer = option3.getText();
System.out.println(" User Selected ANSWER IS : : : :  "+answer);
}
else if(option4.isSelected())
{
System.out.println("Option 4");
answer = option4.getText();
System.out.println(" User Selected ANSWER IS : : : :  "+answer);
}


v1=v1+1;//next ques from database
displayQuestion(v1);//gets the ques from the database and the options also

Answercheck.check(v1); // this only gets the correct answer!!



}
}

also..

v1=0;
so when i run the progg...no ques is displayed..only after i click on b1, is the first question displayed.....

i guess only the order of things is wrong...help me plzzzz!!!

Recommended Answers

All 6 Replies

Well then, display one (or fire an event programettically. Really, the "initial" display never comes as the result of an action event, so, if you want a question displayed right away, than you need to display one, and not only display them in the Action Listener (which won't, obviously, be accessed until an Action Event is fired).

what i want is the answer to be validated...

when i run my progg i get WRONG ANSWER everytime! ! !

Well, that's not what you said

so when i run the progg...no ques is displayed..only after i click on b1, is the first question displayed.....

ill post the whole code!

please please get it to work...i have literally given up now

this is the main progg

package Answer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;


class q extends JFrame implements ActionListener
{

public static String usr,str,opt1,opt2,opt3,opt4,answer;
public static int v1=1;



JMenuBar mbr;
JMenu File,Help;
JPanel p1,p2;
static JLabel l1,l2,l3,l4,l5,l6,l7,l8;
JButton b1,b3;
static JRadioButton option1,option2,option3,option4;
ButtonGroup bgp;
Color c1,c2;
Font f1,f2;

public q()
{

mbr=new JMenuBar();
setJMenuBar(mbr);

File=new JMenu("File");
Help=new JMenu("Help");

p1=new JPanel();
p2=new JPanel();


l1=new JLabel("                    Tech-Quiz");
l2=new JLabel("Q: ");
l3=new JLabel("1.) ");
l4=new JLabel("2.) ");
l5=new JLabel("3.) ");
l6=new JLabel("4.) ");
l7=new JLabel("                                                                                                                               dEaD_SiN");
l8=new JLabel(" ! ");



b1=new JButton("Next Ques");

b3=new JButton("Im too Scared");

c1=new Color(23,54,93);
c2=new Color(255,192,0);


    option1 = new JRadioButton("opt1");
    option2 = new JRadioButton("opt2");
    option3 = new JRadioButton("opt3");
    option4 = new JRadioButton("opt4");
  
  bgp = new ButtonGroup();
   
 bgp.add(option1);
   bgp.add(option2);
   bgp.add(option3);
   bgp.add(option4);



f1=new Font("Broadway",Font.BOLD,72);
f2=new Font("Comic Sans Ms",Font.BOLD,20);

b1.addActionListener(this);

b3.addActionListener(this);

	

	p1.setLayout(new BorderLayout());
	
	l1.setBounds(250,250,300,300);
	l1.setFont(f1);
	l1.setForeground(c2);

	l2.setBounds(50,15,1300,300);
	l2.setFont(f2);
	l2.setForeground(c2);	

	l8.setBounds(100,15,1300,300);
	l8.setFont(f2);
	l8.setForeground(c2);		


	l3.setBounds(50,100,1300,300);
	l3.setFont(f2);
	l3.setForeground(c2);


	l4.setBounds(50,150,1300,300);
	l4.setFont(f2);
	l4.setForeground(c2);

	b1.setBounds(900,280,200,35);
	
	b3.setBounds(900,380,200,35);

	l5.setBounds(50,200,1300,300);
	l5.setFont(f2);
	l5.setForeground(c2);

	l6.setBounds(50,250,1300,300);
	l6.setFont(f2);
	l6.setForeground(c2);

	l7.setBounds(1,250,1300,300);
	l7.setFont(f2);
	l7.setForeground(Color.black);
	l7.setBackground(c2);


        option1.setBounds(150,235,100,30);
        option2.setBounds(150,285,100,30);
        option3.setBounds(150,335,100,30);
        option4.setBounds(150,385,100,30);

	option1.setBackground(c1);
	option1.setForeground(c2);
	option2.setBackground(c1);
	option2.setForeground(c2);	
	option3.setBackground(c1);
	option3.setForeground(c2);	
	option4.setBackground(c1);
	option4.setForeground(c2);


	mbr.add(File);
	mbr.add(Help);



	p2.setLayout(null);
	p2.setBackground(c1);
	p2.add(l2);
	p2.add(l3);
	p2.add(l4);
	p2.add(b1);
	p2.add(l5);
	p2.add(l6);
	p2.add(l8);
	p2.add(b3);
	p2.add(option1);
	p2.add(option2);
	p2.add(option3);
	p2.add(option4);	



	p1.add(l1,BorderLayout.NORTH);
	p1.add(p2,BorderLayout.CENTER);
	p1.add(l7,BorderLayout.SOUTH);
	p1.setBackground(c1);	


	add(p1);
	

















addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});


setVisible(true);
setSize(1280,700);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b3)
{
System.exit(0);
}

else if(ae.getSource()==b1)
{

displayQuestion(v1);

if(option1.isSelected())
{
System.out.println("Option 1");
answer = option1.getText();
System.out.println(" User Selected ANSWER IS : : : :  "+answer);
}
else if(option2.isSelected())
{
System.out.println("Option 2");
answer = option2.getText();
System.out.println(" User Selected ANSWER IS : : : :  "+answer);
}
else if(option3.isSelected())
{
System.out.println("Option 3");
answer = option3.getText();
System.out.println(" User Selected ANSWER IS : : : :  "+answer);
}
else if(option4.isSelected())
{
System.out.println("Option 4");
answer = option4.getText();
System.out.println(" User Selected ANSWER IS : : : :  "+answer);
}

















System.out.println("CORRECT ANSWER IS " +Answercheck.correct);

if(answer==Answercheck.correct)
{
System.out.println("YOUR ANSWER IS CORRECT");
}
else
{
System.out.println("YOUR ANSWER IS WRONG");
}


v1=v1+1;

}
}


public static void displayQuestion(int q1)
{

Answercheck.check(v1);

String dataSourceName = "questions";
String dbURL = "jdbc:odbc:" + dataSourceName;

try { 

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbURL, "",""); 




Statement s = con.createStatement();
s.execute("SELECT Question FROM Ques WHERE ID=" +q1);

ResultSet rs = s.getResultSet();




if (rs != null) // if rs == null, then there is no ResultSet to view
while ( rs.next() ) // this will step through our data row-by-row
{

usr = rs.getString("Question");








}  //while loop ends



l8.setText(usr);
 




s.execute("SELECT opt1,opt2,opt3,opt4 FROM Ques WHERE id=" +q1);
ResultSet pq = s.getResultSet();
if (pq != null)

while ( pq.next() )
{

opt1 = pq.getString("opt1");


opt2 = pq.getString("opt2");


opt3 = pq.getString("opt3");


opt4 = pq.getString("opt4");






}   //while loop ends

option1.setText(opt1);
option2.setText(opt2);
option3.setText(opt3);
option4.setText(opt4);

}   //try block






catch (Exception err)
{
System.out.println( "Error: " + err );
}



}  // function ends




public static void main(String x[])
{
new q();
displayQuestion(v1);
}


} //class ends

this is the Answercheck

package Answer;

import java.sql.*;


class Answercheck 
{
public static String correct;



public static void check(int q1)
{

String dataSourceName = "questions";
String dbURL = "jdbc:odbc:" + dataSourceName;

try { 

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbURL, "",""); 



Statement s = con.createStatement();
s.execute("SELECT copt FROM Ques WHERE ID=" +q1);

ResultSet rs = s.getResultSet();




if (rs != null) // if rs == null, then there is no ResultSet to view

while ( rs.next() ) // this will step through our data row-by-row
{

correct = rs.getString("copt");

//System.out.println("CORRECT OPTION by Answercheck"+correct + "\n");

}  //while loop ends

}   //try block


catch (Exception err)
{
System.out.println( "Error: " + err );
}

}  // function ends


public static void main(String x[])
{
check(1);
check(2);
check(3);
check(4);
check(5);
check(6);
check(7);
check(8);
check(9);
check(10);
}



} //class ends

and when i run it this is what i get

C:\Users\dead_sin\Desktop>java Answer.q
Option 2
User Selected ANSWER IS : : : : Sun
CORRECT ANSWER IS Sun
YOUR ANSWER IS WRONG

Do not use == ( if(answer==Answercheck.correct) ) to compare Strings use the equals method

ohk ill try that and post back!!!

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.