473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pls help with a for loop problem

35
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.  
Jan 19 '08 #1
1 1884
JosAH
11,448 Expert 8TB
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
Jan 19 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
6
by: Ravi | last post by:
Hi All, I am trying to execute a select statement using the DBI module of perl in a for loop. I am getting a strange behaviour, the select statement is excuting correctly only for the last element...
32
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
3
by: deko | last post by:
I have a logging routine that's supposed to silently log errors caught by error handler code on certain functions. The problem is sometimes stuff happens and the error handler can get caught in a...
9
by: No Such Luck | last post by:
I have a function which requires me to loop from the end of a string to the beginning on a char by char basis: int foo (char string) { unsigned int i; for(i = strlen(string); i >= 0; i--) {...
9
by: George McCullen | last post by:
I have an Outlook 2003 using Exchange Server 2003 Public Contacts Folder containing 20,000 Contacts. I am writing a VB .Net 2003 program that loops through all the contacts in a "for each oCt in...
4
by: Bill Moran | last post by:
I've got a bit of a strange problem that's causing me some MAJOR headaches. I'm developing the server-side of a large database application in PostgreSQL. This consists of a C daemon, and a LOT...
4
by: Jacob.Bruxer | last post by:
Hi, I'm pretty new to Visual Basic and was wondering if anyone could give some general advise on how to conserve memory when running a Visual Basic program that I've created. I keep getting a...
9
by: tshad | last post by:
This was posted before but the message got messed up (all NLs were stripped out for some reason). I have 2 labels that hold the name of different images on my .aspx page. <asp:Label ID="Logo"...
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.