I have to write a GUI based quiz game in Java, but I have no idea how to use the following code to finish the program. The questions and answers are in a text file. Please help!!!

public class QnA
{
//index of correct answer when calling the getAnswer() method:
public static final int RIGHT_ANSWER = 5;
private Vector question = new Vector ();//contains the questions
private Vector answer1 = new Vector ();//first possible answer
private Vector answer2 = new Vector ();//second possible answer
private Vector answer3 = new Vector ();//third possible answer
private Vector answer4 = new Vector ();//fourth possible answer
private Vector right = new Vector ();//(index)correct answer 
	
public QnA (String UGGR)
{
	String s;
	/*flag rot determine what is to be read next;
	0 - question
	1 - first answer
	2 - second answer
	3 - third answer
	4 - fourth answer
	5 - correct answer  */
	
	int readNow = 0;
	
	try
	{
	FileReader fsource = new FileReader (UGGR);
	BufferedReader bsource = new BufferedReader (fsource);
		
	while((s = bsource.readLine()) != null)
	{
	s = s.trim(); //remove spaces from both ends of a string
	if (s.length() !=0) //ignores empty lines
		if (readNow == 0)
		{
		question.addElement(s);
		readNow++;
		}
		else if (readNow == 1)
		{
		answer1.addElement(s);
		readNow++;
		}
		else if (readNow == 2)
		{
		answer2.addElement(s);
		readNow++;
		}
		else if (readNow == 3)
		{
		answer3.addElement(s);
		readNow++;
		}
		else if (readNow == 4)
		{
		answer4.addElement(s);
		readNow++;
		}
		else if (readNow == 5)
		{
		right.addElement(s);
		readNow = 0;
		}
	}
			
	bsource.close();
	fsource.close();
			
	if (readNow !=0)
	{
	System.err.println("File" + "UGGR.txt" + ", last question is incomplete!");
	System.exit(1);
	}
}
		
	catch (Exception e)
	{
	System.err.println("Error loading questions: " + e);
	System.exit(1);
	}
	
	if(getNumberOfQuestions() < 1)
	{
	System.err.println("No questions loaded!");
	System.exit(1);
	}
}
	
public int getNumberOfQuestions()
{
	return question.size();
}
	
public String getQuestion(int index)
{
	try
	{
	return (String) question.get(index);
	}
	catch (ArrayIndexOutOfBoundsException e)
	{
	return "NO QUESTION";
	}
}
	
public String getAnswer(int index, int qNumber)
{
	try
	{
	switch(qNumber)
	{
		case 1:
		return (String)answer1.get(index);
		case 2:
		return (String)answer2.get(index);
		case 3:
		return (String)answer3.get(index);
		case 4:
		return (String)answer4.get(index);
		case 5:
		return (String)right.get(index);
	}
}
		
	catch(ArrayIndexOutOfBoundsException e)
	{
	}
	return "NO ANSWER";
       }
}

Recommended Answers

All 5 Replies

There is no GUI, so I'm assuming your job is to make one that plays this game. You're going to need to think out a design to allow the user to play the game for starters. Once you've done that, come back, describe your layout, and I'll help more

It looks like you will create a GUI. Then your GUI will instantiate an object of the QnA class. You will then use the methods of the QnA class to fill in text boxes and get user input from your GUI class.

public class QnAGUI extends JFrame
{
    private QnA qnA;   //an object of the QnA class 

    public QnAGUI(){
         qna = new QnA();//instantiate the object
         //a bunch of other GUI stuff
    }

    //event handlers to interact with the user


   //main method

}

There is no GUI, so I'm assuming your job is to make one that plays this game. You're going to need to think out a design to allow the user to play the game for starters. Once you've done that, come back, describe your layout, and I'll help more

I have a design, but its not totally complete. It will give some idea of how I want the players to react. Just one question, how do you attach a file to these messages?

Do you mean attach the text from a file to the messages (1)? Or attach, say, a link that when clicked opens a file (2)?

(1): Use a JTextArea. There are plenty of tutorials available on the web.

(2): This is a little bit more difficult. There are a few parts you need to do to do this. The first part is creating the JLabel (JLabel is a label that is simply a line of text). To do that, look up JLabel on google. The second part is adding the JLabel to the GUI (also seen in lots of tutorials). The third part is adding a Mouse Listener to the JLabel, so that you can detect when it has been clicked on. The fourth part is writing the code to open the file (this code should be written inside the MouseListener's event listener method). <-- That link only shows you how to add a mouse event listener to a JLabel, the code inside the method is useless to you - you will need to write your own. Here is a code example (from code that I wrote in the past) of how to open a file in the default editor (say, notepad or whatever it is). Oh, and the link "MouseListener's event listener method" is what you'll need to do to add the mouse listener in step three. You can tell where the relevenat code is because it says jLabelName.addMouseListener() - the stuff after that (the code looks weird) is called an anonymous inner class. You can read more about that here: http://java.sun.com/docs/books/tutorial/uiswing/events/generalrules.html

/**
* This code example opens the File named by the String "word".
***/
public static void openDoc(String word)
  {
    try
    {
     //Where word is the [I]pathname[/I] of your file. 
     //So something like C:/All Users/whatever.txt or something.
      Desktop.getDesktop().open(new File(word));
    }
    catch (Exception e)
    {
        //Exception handling in here
    }
  }

I am having a problem getting the components to show up as well as set the size of the window itself. Here is the code, could you help please? The window comes up just fine.

public class UGRRGUI extends JFrame 
{
	 static String answer1 = "answer1";
	 static String answer2 = "answer2";
	 static String answer3 = "answer3";
	 static String answer4 = "answer4";
	 static String question = "question";
	 
    private UGRRGUI qna;   //an object of the UGRR class 

    public UGRRGUI()
	 {
       qna = new UGRRGUI();//instantiate the object
   
	      //a bunch of other GUI stuff
	 }

 /** Listens to the radio buttons. */
public void actionPerformed(ActionEvent e)
	 {
        
    }

private static void makeShowGUI()
  {
        //Create and set up the window.
        JFrame frame = new JFrame("Underground Railroad Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

       //main method
 public static void main(String[] args) 
	 {
        makeShowGUI();
    }
}
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.