For loop problem

Reply

Join Date: Jan 2008
Posts: 2
Reputation: saytri is an unknown quantity at this point 
Solved Threads: 0
saytri saytri is offline Offline
Newbie Poster

For loop problem

 
0
  #1
Jan 19th, 2008
I am doing a quiz where the user can choose from 3 different quizzez. My tutor told me to use binary files to store the questions. I have succesful done this. The answers are stored in an array. My problem is with a for loop that i have done, to check whether the answers entered by the user are correct or not. But the program isn't doing anything inside the for loop. Can someone pls help me. Thanks a lot. Source code examples are really helpful to me.

This is the code:
  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. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.DataOutputStream;
  14. import java.io.FileNotFoundException;
  15. import java.io.IOException;
  16.  
  17.  
  18.  
  19. public class GQ extends JFrame implements ActionListener {
  20. private static final int FRAME_WIDTH = 1024;
  21. private static final int FRAME_HEIGHT = 768;
  22. private static final int FRAME_X_ORIGIN = 0;
  23. private static final int FRAME_Y_ORIGIN = 0;
  24.  
  25.  
  26. AnswerStore answerStore = new AnswerStore();
  27. boolean timeForMore;
  28.  
  29.  
  30.  
  31. public GQ() {
  32.  
  33.  
  34. Container contentPane;
  35. contentPane = getContentPane();
  36.  
  37. JButton button1, button2, button3, button4, button5;
  38.  
  39. setSize (FRAME_WIDTH, FRAME_HEIGHT);
  40. setTitle("Geography Quiz");
  41. setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
  42.  
  43.  
  44. button1 = new JButton("Plate Tectonics");
  45. button2 = new JButton("Rivers");
  46. button3 = new JButton("Rocks");
  47. button4 = new JButton("Quit");
  48.  
  49. contentPane.add(button1);
  50. contentPane.add(button2);
  51. contentPane.add(button3);
  52. contentPane.add(button4);
  53.  
  54. button1.addActionListener(this);
  55. button1.setActionCommand("b1");
  56. button2.addActionListener(this);
  57. button2.setActionCommand("b2");
  58. button3.addActionListener(this);
  59. button3.setActionCommand("b3");
  60. button4.addActionListener(this);
  61. button4.setActionCommand("b4");
  62. setDefaultCloseOperation(EXIT_ON_CLOSE);
  63.  
  64.  
  65. public static void main (String[] args) {
  66.  
  67. JOptionPane.showMessageDialog(null, "This is a Geography Quiz");
  68. JOptionPane.showMessageDialog(null, "Good Luck");
  69.  
  70.  
  71. GQ frame = new GQ();
  72. frame.setVisible(true);
  73.  
  74. new GQ();
  75. GQ File = new GQ();
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. String ac = event.getActionCommand();
  87. String[] question = null;
  88. String[] answers = null;
  89.  
  90.  
  91. if(ac.equals("b2")) {
  92. final JPF1 appRef = new JPF1();
  93. JFrame frame = new JFrame("CountDown");
  94. MP panel = new MP();
  95. panel.addCountDownListener(appRef);
  96. panel.setVisible(true);
  97. frame.setSize(panel.getSize());
  98. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  99. frame.setContentPane(panel);
  100. frame.setVisible(true);
  101.  
  102. question = readFile("rivers.dat");
  103. answers = answerStore.riverAnswers;
  104.  
  105.  
  106.  
  107. } else if (ac.equals("b4")) {
  108. System.exit(0);
  109. }
  110.  
  111.  
  112. askQuestions(question, answers); // calls the questions from textfile
  113.  
  114.  
  115. }
  116. public void stopAndShowResults() {
  117.  
  118. timeForMore = false; // used for countdown.
  119. }
  120.  
  121. public String[] readFile(String path) {
  122. StringBuilder sb = new StringBuilder();
  123. String separator = "\n";
  124. String question;
  125. try{
  126.  
  127. File aFile = new File( "rivers.dat" );
  128. // create an output stream to the file
  129. FileInputStream aFileInStream = new FileInputStream ( aFile );
  130. // create a data output stream to the file output stream
  131. DataInputStream aDataInStream = new DataInputStream ( aFileInStream );
  132.  
  133. // read data from file
  134. question = aDataInStream.readUTF();
  135.  
  136.  
  137. aFileInStream.close();
  138. JOptionPane.showInputDialog(null,question);
  139.  
  140.  
  141.  
  142. }
  143. catch( FileNotFoundException e )
  144. {
  145. System.out.println( e.getMessage() );
  146. System.exit(1);
  147. }
  148.  
  149. catch(IOException e)
  150. {
  151. System.out.println( e.getMessage() );
  152. System.exit(1);
  153. }
  154.  
  155. return sb.toString().split("\\n");
  156. }
  157.  
  158.  
  159. public void askQuestions(String[] question, String[] answers) {
  160.  
  161. int count = 0;
  162. int point = 0;
  163.  
  164.  
  165. for(int j = 0; j < question.length; j++) { this for loop isn't working
  166.  
  167. timeForMore = true;
  168. String input = JOptionPane.showInputDialog(null,question[j]);// shows questions in a dialog box together with input line
  169.  
  170. JOptionPane.showInputDialog(null,question);
  171. if(answers[j].equals(input))
  172. {
  173.  
  174. count++; // incrementing counter if entered answer is correct
  175. point++;
  176. }
  177. if(!timeForMore) // if time is over, the program executes the loop an stops asking questions.
  178. break;
  179. }
  180.  
  181.  
  182. JOptionPane.showMessageDialog(null, "You answered " + count +
  183. " out of " + question.length +
  184. " questions correctly.");
  185. int percent = (count*100)/question.length;
  186. JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + percent + " % ");
  187.  
  188.  
  189.  
  190.  
  191. class AnswerStore { // storing answers of each quiz in arrays
  192.  
  193.  
  194. String[] riverAnswers = {
  195. "Gorges", "Meanders", "Levees", "Yes", "Less Economic Developed Countries",
  196. "crescent shaped lakes", "More Economic Developed Countries", "No", "River Discharge", "No", "",
  197. "", ""
  198. };
  199.  
  200.  
  201.  
  202.  
  203. }
  204.  
  205. }
  206.  
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: For loop problem

 
0
  #2
Jan 20th, 2008
My problem is with a for loop that i have done, to check whether the answers entered by the user are correct or not. But the program isn't doing anything inside the for loop.
Please try to be more specific regarding the behaviour of your program. Mention what exactly it does when you run it and how that differs from your requirements.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,126
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 472
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: For loop problem

 
0
  #3
Jan 20th, 2008
saytri I run your code but there is to many errors coming out. Either you gave us wrong version or you tried to trick us in debuging your code. Which one of these?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2
Reputation: saytri is an unknown quantity at this point 
Solved Threads: 0
saytri saytri is offline Offline
Newbie Poster

Re: For loop problem

 
0
  #4
Jan 21st, 2008
That is the code i have. But i think the problem is because i have other classes and so when you paste it on your java program, its not finding them. But don't worry, i have solved the problem now. I just have to ask one question. I have a set of questions in a binary file. And i wish to generate them randomly. I have done this, but the problem is that some of the questions are being shown repetitvley. Is there maybe another method that i can use that doesn't display the question repetively? Thanks a lot. :-)

This is the code i did:

  1. public void askQuestions(String[] questions, String[] answers) {
  2.  
  3. int count = 0;
  4. int point = 0;
  5.  
  6.  
  7. for(int j = 0; j < questions.length; j++) {
  8.  
  9. timeForMore = true;
  10.  
  11.  
  12. Random generator = new Random();
  13. int randomIndex = generator.nextInt(questions.length);
  14. String input = JOptionPane.showInputDialog(null, questions[randomIndex]);
  15.  
  16.  
  17. if(answers[randomIndex].equalsIgnoreCase(input))
  18.  
  19. count++; // incrementing counter if entered answer is correct
  20. point++;
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. if(!timeForMore) // if time is over, the program executes the loop an stops asking questions.
  29. break;
  30. }
  31.  
  32.  
  33.  
  34.  
  35.  
  36. JOptionPane.showMessageDialog(null, "You answered " + count +
  37. " out of " + questions.length +
  38. " questions correctly.");
  39. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,126
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 472
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: For loop problem

 
0
  #5
Jan 21st, 2008
1) Create array of integers that will have same number of elements as you question array
2) Call random function to get number
3) Check with existing elements in the integer array if the number is not already in array.
4) If not add it to array,
Else number already exists cast new number
5) Repeat till you fill your array

Let say you end up with something like this {3, 5, 0, 2, 1, 4}. Then loop through this array and display your questions.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC