Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-22-2007, 12:51 PM
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
Help pls with a quiz
Hi i'm doing a quiz using java. I have wrote the questions in a textfile and i called them from Java. But now my problem is about the answers. i stored the answers in another class, and i used arrays to store the answers. Now what i'm having trouble with is, that when the user enters the answer i want to compare the answer with the correct answer so that if he/she answers correctly i would display correct else it would display incorrect. Can someone pls help, by showing me how to compare the answers. I tried using if condition but i diddn't know what conditions i had to compare. Thanks a lot for the help.

This is the code i have written:

Code:
import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import java.util.Scanner; import java.util.Arrays; public class GeographyQuiz extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 140; private static final int FRAME_HEIGHT = 160; private static final int FRAME_X_ORIGIN = 70; private static final int FRAME_Y_ORIGIN = 50; public static void main (String[] args) { JFrame jFrame; jFrame = new JFrame(); JOptionPane.showMessageDialog(jFrame, "This is a Geography Quiz"); JOptionPane.showMessageDialog(null, "Good Luck"); char choice; int i, choice1, Password; String yourChoice, passString; passString = JOptionPane.showInputDialog("Enter the Password"); //Password = passString.nextInt(); Password = Integer.parseInt(passString); if (Password == 123) { JOptionPane.showMessageDialog(null, "Valid. You typed the right password. Now choose from the following menu"); GeographyQuiz frame = new GeographyQuiz(); frame.setVisible(true); } else { JOptionPane.showMessageDialog(null, "Invalid Password. Try Again"); } } public GeographyQuiz() { Container contentPane; JButton button1, button2, button3, button4, button5; setSize (FRAME_WIDTH, FRAME_HEIGHT); setTitle("Geography Quiz"); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane = getContentPane(); contentPane.setBackground(Color.pink); contentPane.setLayout(new FlowLayout()); button1 = new JButton("Plate Tectonics"); button2 = new JButton("Rivers"); button3 = new JButton("Rocks"); button4 = new JButton("Quit"); contentPane.add(button1); contentPane.add(button2); contentPane.add(button3); contentPane.add(button4); button1.addActionListener(this); button1.setActionCommand("b1"); button2.addActionListener(this); button2.setActionCommand("b2"); button3.addActionListener(this); button3.setActionCommand("b3"); button4.addActionListener(this); button4.setActionCommand("b4"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if ("b1".equals(e.getActionCommand())) { Scanner s = null; try { s = new Scanner(new BufferedReader(new FileReader("plate_tectonics.txt"))); while (s.hasNext()) { s.useDelimiter(",\\s*"); JOptionPane.showInputDialog(null,s.nextLine()); JOptionPane.showMessageDialog(null, "Correct"); } } catch (java.io.FileNotFoundException f) { JOptionPane.showMessageDialog(null, "File not found."); } finally { if (s != null) s.close(); } } else if("b2".equals(e.getActionCommand())) { Scanner s = null; try { s = new Scanner(new BufferedReader(new FileReader("rivers.txt"))); while (s.hasNext()) { s.useDelimiter(",\\s*"); JOptionPane.showInputDialog(null,s.nextLine()); } } catch (java.io.FileNotFoundException f) { JOptionPane.showMessageDialog(null, "File not found."); } finally { if (s != null) s.close(); } } else if("b3".equals(e.getActionCommand())) { Scanner s = null; try { s = new Scanner(new BufferedReader(new FileReader("rocks.txt"))); while (s.hasNext()) { s.useDelimiter(",\\s*"); JOptionPane.showInputDialog(null,s.nextLine()); } } catch (java.io.FileNotFoundException f) { JOptionPane.showMessageDialog(null, "File not found."); } finally { if (s != null) s.close(); } } else if ("b4".equals(e.getActionCommand())) { System.exit(0); } } }
And this is the other class which stores the anwers to the questions in an array:

Code:
import java.util.*; public class Answer { public static void main (String[] args) { String[] answer = new String[10]; answer[0] = "Hellenic"; answer[1] = "constructive"; answer[2] = "100km"; answer[3] = "Italy"; answer[4] = "Wegner"; answer[5] = "constructive"; answer[6] = "100km"; answer[7] = "Italy"; answer[8] = "destroyed"; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-22-2007, 01:29 PM
khamuruddeen's Avatar
Member
 
Join Date: Dec 2007
Posts: 24
khamuruddeen is on a distinguished road
i think it is with using any datbase like mysql..etc

It is better 2 store the answers in database with some reference as
questionid answer like that then u sholul take the answers from that database with the reference of the question id like that...

I think it is the easy proscess if u know the database..
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-22-2007, 04:39 PM
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
The problem is that i don't know how to use database much.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-23-2007, 07:09 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
An easy way is to store both the questions and answers in a file and read both into arrays at the same time. Then you can pass both arrays to the askQuestions method.
Otherwise, you can try something like this (which has not been compiled/tested - no file data) which answers your question about how to access the answer class data from within GQ.
Code:
import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import javax.swing.*; public class GQ extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 140; private static final int FRAME_HEIGHT = 160; private static final int FRAME_X_ORIGIN = 70; private static final int FRAME_Y_ORIGIN = 50; // Instantiate your answer class here so you can access // its fields in this class. AnswerStore answerStore = new AnswerStore(); public static void main (String[] args) { JOptionPane.showMessageDialog(jFrame, "This is a Geography Quiz"); JOptionPane.showMessageDialog(null, "Good Luck"); String passString = JOptionPane.showInputDialog("Enter the Password"); //Password = passString.nextInt(); int Password = Integer.parseInt(passString); if (Password == 123) { JOptionPane.showMessageDialog(null, "Valid. You typed the right password. " + "Now choose from the following menu"); GQ frame = new GQ(); frame.setVisible(true); } else { JOptionPane.showMessageDialog(null, "Invalid Password. Try Again"); } } public GQ() { Container contentPane; JButton button1, button2, button3, button4, button5; setSize (FRAME_WIDTH, FRAME_HEIGHT); setTitle("Geography Quiz"); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane = getContentPane(); contentPane.setBackground(Color.pink); contentPane.setLayout(new FlowLayout()); button1 = new JButton("Plate Tectonics"); button2 = new JButton("Rivers"); button3 = new JButton("Rocks"); button4 = new JButton("Quit"); contentPane.add(button1); contentPane.add(button2); contentPane.add(button3); contentPane.add(button4); button1.addActionListener(this); button1.setActionCommand("b1"); button2.addActionListener(this); button2.setActionCommand("b2"); button3.addActionListener(this); button3.setActionCommand("b3"); button4.addActionListener(this); button4.setActionCommand("b4"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); String[] questions = null; if (ac.equals("b1")) { questions = readFile("plate_tectonics.txt"); } else if(ac.equals("b2")) { questions = readFile("rivers.txt"); } else if(ac.equals("b3")) { questions = readFile("rocks.txt"); } else if (ac.equals("b4")) { System.exit(0); } askQuestions(questions); } private String[] readFile(String path) { Scanner s = null; StringBuilder sb = new StringBuilder(); String separator = "\n" try { s = new Scanner(new BufferedReader(new FileReader(path))); s.useDelimiter(",\\s*"); while (s.hasNext()) { sb.append(s.next() + " "); // JOptionPane.showMessageDialog(null,s.next()); } } finally { if (s != null) s.close(); break; } return sb.toString().split("\\s"); } private void askQuestions(String[] questions) { int count = 0; for(int j = 0; j < questions.length; j++) { String input = JOptionPane.showInputDialog(null, questions[j]); // Use reference to AnswerStore instance to access data in it. if(answerStore.answers[j].equals(input)) count++; } JOptionPane.showMessageDialog(null, "You answered " + count + " out of " + questions.length + " questions correctly."); } } class AnswerStore { String[] answer = { "Hellenic", "constructive", "100km", "Italy", "Wegner", "constructive", "100km", "Italy", "destroyed" }; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 10:59 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org