Hey .. Everyone ...

I'm now to java .. I'm trying to figure out .. how everything fits together ..
well .. i have a project that I'm working on .. .. basically it a type of quiz working on java .. where a user takes a simple MCQ quiz and they have to answer it in 20 sec or else the .. quiz moves on to the next question .. im not using .. any .. Database connectivity .. but instead im using file management and file operations to read the questions from the file and displaying it.. to the user .... and right now .. this is the place where i'm stuck ..

well here is my questions Form..

package Prj;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.awt.*;
import java.awt.Event.*;

public class TestPage extends JFrame {
        Timer timer;
	int count = 20 ;
        JButton submit ,skip ;
 static JLabel timerLabel , ques;
         JRadioButton opt1 , opt2 , opt3 , opt4 ;
         ButtonGroup bgp;
         static int quesNo , correctAns ;




public TestPage(int a ){
    quesNo = a ;
    submit = new JButton("Submit");
    skip = new JButton("Skip");
    ques = new JLabel("Ques");
    opt1 = new JRadioButton("opt1");
    opt2 = new JRadioButton("opt2");
    opt3 = new JRadioButton("opt3");
    opt4 = new JRadioButton("opt4");
    bgp = new ButtonGroup();
    bgp.add(opt1);
    bgp.add(opt2);
    bgp.add(opt3);
    bgp.add(opt4);




    
    
    
    //setVisible(true);
    setResizable(false);
    
    setLayout(null);
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        timer  = new Timer(1000,new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        
          if(count==0){
               timer.stop();
                    new TestPage(quesNo++);

          }
          else
		count--;
		timerLabel.setText(Integer.toString(count));
      }
    });
        timer.start();

	timerLabel = new JLabel(Integer.toString(count));
        timerLabel.setForeground(Color.red);
        timerLabel.setFont(new Font("Times New Roman",Font.BOLD,25));

        timerLabel.setBounds(450,25,50,25);

        skip.setBounds(250,420,100,30);
        submit.setBounds(350,420,100,30);


        questionSelect();

        submit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                Boolean a=checkAns();
                if(a)
                    new TestPage(quesNo++);
                setVisible(false);




            }
        });

        opt1.setBounds(330,180,100,30);
        opt2.setBounds(330,210,100,30);
        opt3.setBounds(330,240,100,30);
        opt4.setBounds(330,270,100,30);
        ques.setBounds(50,100,400,30);


        add(timerLabel);
	add(submit);
        add(skip);
        add(opt4);
        add(opt3);
        add(opt2);
        add(opt1);
        add(ques);


        pack();
        setSize(500,500);
        setVisible(true);
        
}

boolean checkAns(){
    boolean a = false ;
    switch(correctAns)
    {
        case 1:
            if ( opt1.isSelected() )
                a = true ;
            break;
        case 2:
            if ( opt2.isSelected() )
                a = true ;
            break;
        case 3:
            if ( opt3.isSelected() )
                a = true ;
            break;
        case 4:
            if ( opt4.isSelected() )
                a = true ;
            break;
        default:
            System.out.println("Error");



    }
    return a ;


}

void questionSelect(){
    FileHandling.readFromQFile();

    ques.setText(FileHandling.y[quesNo][0]);
    opt1.setText(FileHandling.y[quesNo][1]);
    opt2.setText(FileHandling.y[quesNo][2]);
    opt3.setText(FileHandling.y[quesNo][3]);
    opt4.setText(FileHandling.y[quesNo][4]);

    correctAns = Integer.parseInt(FileHandling.y[quesNo][5]);
}


  public static void main(String args[]) {

 new TestPage(0);
 
  }
}

along with this .. i have a file .. which contains .. all the questions and options and along with it it contains the correct answer from all the options .. the file which has all the questions .. is like this

Who Made Java ? &Oracle&Sun&Microsoft&Intel&2&Where is London ?&US&UK&France&Australia&2&

it contains all the Questions and options related to the question separated by the "?" sign. .. I'm reading the data from .. the file and storing it in a double dimensional array .. so that .. it can be .. allotted to the given form ..

the problems .. I'm facing are
the program is not skipping to the next question..after 20 seconds ..and shows the same question and the same options again .. after 20 seconds ..

please help me in this ... if u need anything else .. like the file operation code s.. let me know

Hi,

I'm pretty sure the problem is when you create a new TestPage. Look at line 56 (and also line 84): new TestPage(quesNo++);
The ++ operator increments quesNo AFTER this operation. If you want to increment it before use ++quesNo instead.

thanks a lot di2daer ...


worked like a charm ..

just got one more problem ..
it takes a lot of time .. when i enter the wrong answer .. any .. suggestions

And if it is wrong, what do you do? Your code does nothing but set visibility to false after one answers. Not sure why...

Thank You Mr Taywin ....

You Enlightened .. me ..
now Figured out a new way of traversing to next question

This article has been dead for over six months. Start a new discussion instead.