| | |
For loop problem
![]() |
•
•
Join Date: Jan 2008
Posts: 2
Reputation:
Solved Threads: 0
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:
This is the code:
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import java.awt.event.ActionListener.*; import java.util.*; import java.io.*; import javax.swing.*; import java.util.Calendar; import java.awt.image.*; import javax.imageio.*; import java.io.File; import java.io.FileOutputStream; import java.io.DataOutputStream; import java.io.FileNotFoundException; import java.io.IOException; public class GQ extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 1024; private static final int FRAME_HEIGHT = 768; private static final int FRAME_X_ORIGIN = 0; private static final int FRAME_Y_ORIGIN = 0; AnswerStore answerStore = new AnswerStore(); boolean timeForMore; public GQ() { Container contentPane; contentPane = getContentPane(); JButton button1, button2, button3, button4, button5; setSize (FRAME_WIDTH, FRAME_HEIGHT); setTitle("Geography Quiz"); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); 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 static void main (String[] args) { JOptionPane.showMessageDialog(null, "This is a Geography Quiz"); JOptionPane.showMessageDialog(null, "Good Luck"); GQ frame = new GQ(); frame.setVisible(true); new GQ(); GQ File = new GQ(); } String ac = event.getActionCommand(); String[] question = null; String[] answers = null; if(ac.equals("b2")) { final JPF1 appRef = new JPF1(); JFrame frame = new JFrame("CountDown"); MP panel = new MP(); panel.addCountDownListener(appRef); panel.setVisible(true); frame.setSize(panel.getSize()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.setVisible(true); question = readFile("rivers.dat"); answers = answerStore.riverAnswers; } else if (ac.equals("b4")) { System.exit(0); } askQuestions(question, answers); // calls the questions from textfile } public void stopAndShowResults() { timeForMore = false; // used for countdown. } public String[] readFile(String path) { StringBuilder sb = new StringBuilder(); String separator = "\n"; String question; try{ File aFile = new File( "rivers.dat" ); // create an output stream to the file FileInputStream aFileInStream = new FileInputStream ( aFile ); // create a data output stream to the file output stream DataInputStream aDataInStream = new DataInputStream ( aFileInStream ); // read data from file question = aDataInStream.readUTF(); aFileInStream.close(); JOptionPane.showInputDialog(null,question); } catch( FileNotFoundException e ) { System.out.println( e.getMessage() ); System.exit(1); } catch(IOException e) { System.out.println( e.getMessage() ); System.exit(1); } return sb.toString().split("\\n"); } public void askQuestions(String[] question, String[] answers) { int count = 0; int point = 0; for(int j = 0; j < question.length; j++) { this for loop isn't working timeForMore = true; String input = JOptionPane.showInputDialog(null,question[j]);// shows questions in a dialog box together with input line JOptionPane.showInputDialog(null,question); if(answers[j].equals(input)) { count++; // incrementing counter if entered answer is correct point++; } if(!timeForMore) // if time is over, the program executes the loop an stops asking questions. break; } JOptionPane.showMessageDialog(null, "You answered " + count + " out of " + question.length + " questions correctly."); int percent = (count*100)/question.length; JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + percent + " % "); class AnswerStore { // storing answers of each quiz in arrays String[] riverAnswers = { "Gorges", "Meanders", "Levees", "Yes", "Less Economic Developed Countries", "crescent shaped lakes", "More Economic Developed Countries", "No", "River Discharge", "No", "", "", "" }; } }
•
•
•
•
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.
"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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jan 2008
Posts: 2
Reputation:
Solved Threads: 0
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:
This is the code i did:
Java Syntax (Toggle Plain Text)
public void askQuestions(String[] questions, String[] answers) { int count = 0; int point = 0; for(int j = 0; j < questions.length; j++) { timeForMore = true; Random generator = new Random(); int randomIndex = generator.nextInt(questions.length); String input = JOptionPane.showInputDialog(null, questions[randomIndex]); if(answers[randomIndex].equalsIgnoreCase(input)) count++; // incrementing counter if entered answer is correct point++; if(!timeForMore) // if time is over, the program executes the loop an stops asking questions. break; } JOptionPane.showMessageDialog(null, "You answered " + count + " out of " + questions.length + " questions correctly."); }
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.
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- nested for loop problem (Java)
- Strange Excel infinite-loop problem (Windows Software)
- Cold Fusion Loop problem (ColdFusion)
- Help w/ for loop. (C++)
- loop problem (C)
- Random Different Number in each loop ?? HLP Me PLZ (c or c++) (C++)
- loop problem (C++)
Other Threads in the Java Forum
- Previous Thread: tuff question in Java
- Next Thread: Need help with getting java and classpaths
| Thread Tools | Search this Thread |
addball android api applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse ee error eventlistener exception expand fractal free game gis givemetehcodez graphics gui guidancer html ide image inetaddress integration intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia jvm linux loan loop map method migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program project radio scanner server service set sharepoint smart sms smsspam software sortedmaps sql subclass support swing textfield threads tree trolltech unlimited utility windows






