Connecting Tech Pros Worldwide Help | Site Map

Pls help with a for loop problem

Member
 
Join Date: Dec 2007
Posts: 35
#1: Jan 19 '08
I have a problem with this code. I'm, doing a quiz, and the questions are stored in a binary file. My problem is how am i going to compare the answers entered by the user to the correct answers. The correct answers are stored in an array. Pls can someone help me with this code, because i'm really stuck. I think the problem is in the for loop (the code below). Thanks a lot. If you help me with examples of source code, i appreciate it a lot. Thanks again.


Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.event.ActionListener.*;
  4. import java.util.*;
  5. import java.io.*;
  6. import javax.swing.*;
  7. import java.util.Calendar;
  8. import java.awt.image.*;
  9. import javax.imageio.*;
  10.  
  11.  
  12. public class GQ extends JFrame implements ActionListener {
  13.     private static final int FRAME_WIDTH = 1024;
  14.     private static final int FRAME_HEIGHT = 768;
  15.     private static final int FRAME_X_ORIGIN = 0;
  16.     private static final int FRAME_Y_ORIGIN = 0;
  17.  
  18.     private JLabel prompt;
  19.     private JPanel image;
  20.     private JLabel response;
  21.     private JMenu  fileMenu;
  22.     private JMenu  HelpMenu;
  23.  
  24.     AnswerStore answerStore = new AnswerStore();
  25.     boolean timeForMore;
  26.  
  27.  
  28.   public GQ() {
  29.  
  30.  
  31.         Container contentPane;  
  32.         contentPane = getContentPane();
  33.  
  34.         JButton button1, button2, button3, button4, button5;
  35.  
  36.         setSize (FRAME_WIDTH, FRAME_HEIGHT);
  37.         setTitle("Geography Quiz");
  38.         setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
  39.  
  40.         createFileMenu();
  41.         createHelpMenu();
  42.  
  43.         JMenuBar menuBar= new JMenuBar();
  44.         setJMenuBar(menuBar);
  45.         menuBar.add(fileMenu);
  46.         menuBar.add(HelpMenu);
  47.  
  48.         response=new JLabel();
  49.         response.setBounds(50, 50, 250, 50);
  50.         contentPane.add(response);
  51.  
  52.         response=new JLabel();
  53.         response.setBounds(50, 50, 250, 50);
  54.         contentPane.add(response);
  55.  
  56.         button1 = new JButton("Plate Tectonics");
  57.         button2 = new JButton("Rivers");
  58.         button3 = new JButton("Rocks");
  59.         button4 = new JButton("Quit");
  60.  
  61.  
  62.  
  63.  
  64.         contentPane.add(button1);
  65.         contentPane.add(button2);
  66.         contentPane.add(button3);
  67.         contentPane.add(button4);
  68.  
  69.         button1.addActionListener(this);
  70.         button1.setActionCommand("b1");
  71.         button2.addActionListener(this);
  72.         button2.setActionCommand("b2");
  73.         button3.addActionListener(this);
  74.         button3.setActionCommand("b3");
  75.         button4.addActionListener(this);
  76.         button4.setActionCommand("b4");
  77.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  78.  
  79.  
  80.  
  81.  
  82.     }
  83.  
  84.     public static void main (String[] args) {
  85.  
  86.         JOptionPane.showMessageDialog(null, "This is a Geography Quiz");
  87.         JOptionPane.showMessageDialog(null, "Good Luck");
  88.  
  89.  
  90.             GQ frame = new GQ();
  91.             frame.setVisible(true);
  92.  
  93.             new GQ();
  94.             GQ File = new GQ();
  95.  
  96.       }
  97.  
  98.         public void actionPerformed(ActionEvent event) {
  99.         String menuName;
  100.         JFileChooser chooser;
  101.  
  102.         int status;
  103.  
  104.         chooser=new JFileChooser();
  105.  
  106.  
  107.  
  108.  
  109.     }       
  110.  
  111.         String ac = event.getActionCommand();
  112.         String[] questions = null;
  113.         String[] answers = null;
  114.         if (ac.equals("b1")) {
  115.  
  116.  
  117.              questions = readFile("plate_tectonics.dat");
  118.              answers = answerStore.tectonicAnswers;
  119.  
  120.         } else if(ac.equals("b2")) {
  121.  
  122.  
  123.             questions = readFile("rivers.dat");
  124.             answers = answerStore.riverAnswers;
  125.  
  126.         } else if(ac.equals("b3")) {
  127.  
  128.              questions = readFile("rocks.dat");
  129.              answers = answerStore.rockAnswers;
  130.  
  131.         } else if (ac.equals("b4")) {
  132.             System.exit(0);
  133.            }
  134.  
  135.  
  136.          askQuestions(questions, answers); // calls the questions from textfile
  137.  
  138.  
  139.     }
  140.  
  141.  
  142.  
  143.     public void stopAndShowResults() {
  144.  
  145.         timeForMore = false;  // used for countdown. 
  146.     }
  147.  
  148.  
  149.  
  150.  
  151.  
  152.     private String[] readFile(String path) {
  153.  
  154.         StringBuilder sb = new StringBuilder();
  155.         String separator = "\n";
  156.         String question, question1, question3;
  157.      try{
  158.  
  159.       File aFile  = new File( "rivers.dat" );
  160.       // create an output stream to the file
  161.       FileInputStream aFileInStream = new FileInputStream ( aFile );
  162.       // create a data output stream to the file output stream
  163.       DataInputStream aDataInStream = new DataInputStream ( aFileInStream );
  164.  
  165.       // read data from file
  166.          question   = aDataInStream.readUTF();
  167.          question1   = aDataInStream.readUTF();
  168.          question3   = aDataInStream.readUTF();
  169.  
  170.          aFileInStream.close();
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.        JOptionPane.showInputDialog(null,question);
  178.  
  179.        JOptionPane.showInputDialog(null,question1);
  180.  
  181.        JOptionPane.showInputDialog(null,question3);
  182.  
  183.  
  184.     }
  185.           catch( FileNotFoundException e )
  186.       {
  187.          System.out.println( e.getMessage() );
  188.          System.exit(1);
  189.       }    
  190.  
  191.       catch(IOException e)
  192.       {
  193.          System.out.println( e.getMessage() );
  194.          System.exit(1);
  195.       }
  196.  
  197.         return sb.toString().split("\\n");
  198.     }
  199.  
  200.     public void askQuestions(String[] questions, String[] answers) {
  201.  
  202.         int count = 0;
  203.         int point = 0;
  204.  
  205.  
  206.        for(int j = 0; j < questions.length; j++) {  // i think i have to change the for loop here
  207.  
  208.            timeForMore = true;
  209.            String input = JOptionPane.showInputDialog(null, questions[j]); // shows questions in a dialog box together with input line
  210.  
  211.             if(answers[j].equals(input))
  212.               { 
  213.  
  214.                 count++;  // incrementing counter if entered answer is correct
  215.                 point++;
  216.              }
  217.             if(!timeForMore) // if time is over, the program executes the loop an stops asking questions.
  218.                 break; 
  219.      }
  220.  
  221.  
  222.  
  223.              try {
  224.  
  225.             JOptionPane.showMessageDialog(null, "You answered " + count +
  226.                                       " out of " + questions.length +
  227.                                       " questions correctly.");
  228.             JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + ((point*100)/10) + " % ");
  229.      }
  230.  
  231.  
  232. class AnswerStore  {  // storing answers of each quiz in arrays
  233.     String[] tectonicAnswers = {
  234.         "Hellenic", "destructive", "100km", "Italy", "Wegner",
  235.         "Mariana", "Sicily", "created", "constructive", "Mediterranean"
  236.     };
  237.  
  238.     String[] riverAnswers = {
  239.         "Gorges", "Meanders", "Levees", "Yes", "Less Economic Developed Countries",
  240.         "crescent shaped lakes", "More Economic Developed Countries", "No", "River Discharge", "No"
  241.     };
  242.  
  243.     String[] rockAnswers = {
  244.         "40km", "Igneous Rock", "Sedimentary", "Basalt", "Organic",
  245.         "pressure", "Oolites", "Igneous", "dark black", "basalt"
  246.     };
  247.  
  248.  
  249.      }
  250.  
  251. }
  252.  
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jan 19 '08

re: Pls help with a for loop problem


Remember that System.out.println() is a fine poor man's debugger; i.e. before
you enter that suspicious loop print out all the questions and answers that were
passed to that method.

If that doesn't help you enough sprinkle in more System.out.println() statements
near every part you're not sure off.

kind regards,

Jos
Reply