Pls help with a for loop problem | Member | | Join Date: Dec 2007
Posts: 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. - 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.*;
-
-
-
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;
-
-
private JLabel prompt;
-
private JPanel image;
-
private JLabel response;
-
private JMenu fileMenu;
-
private JMenu HelpMenu;
-
-
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);
-
-
createFileMenu();
-
createHelpMenu();
-
-
JMenuBar menuBar= new JMenuBar();
-
setJMenuBar(menuBar);
-
menuBar.add(fileMenu);
-
menuBar.add(HelpMenu);
-
-
response=new JLabel();
-
response.setBounds(50, 50, 250, 50);
-
contentPane.add(response);
-
-
response=new JLabel();
-
response.setBounds(50, 50, 250, 50);
-
contentPane.add(response);
-
-
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();
-
-
}
-
-
public void actionPerformed(ActionEvent event) {
-
String menuName;
-
JFileChooser chooser;
-
-
int status;
-
-
chooser=new JFileChooser();
-
-
-
-
-
}
-
-
String ac = event.getActionCommand();
-
String[] questions = null;
-
String[] answers = null;
-
if (ac.equals("b1")) {
-
-
-
questions = readFile("plate_tectonics.dat");
-
answers = answerStore.tectonicAnswers;
-
-
} else if(ac.equals("b2")) {
-
-
-
questions = readFile("rivers.dat");
-
answers = answerStore.riverAnswers;
-
-
} else if(ac.equals("b3")) {
-
-
questions = readFile("rocks.dat");
-
answers = answerStore.rockAnswers;
-
-
} else if (ac.equals("b4")) {
-
System.exit(0);
-
}
-
-
-
askQuestions(questions, answers); // calls the questions from textfile
-
-
-
}
-
-
-
-
public void stopAndShowResults() {
-
-
timeForMore = false; // used for countdown.
-
}
-
-
-
-
-
-
private String[] readFile(String path) {
-
-
StringBuilder sb = new StringBuilder();
-
String separator = "\n";
-
String question, question1, question3;
-
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();
-
question1 = aDataInStream.readUTF();
-
question3 = aDataInStream.readUTF();
-
-
aFileInStream.close();
-
-
-
-
-
-
-
JOptionPane.showInputDialog(null,question);
-
-
JOptionPane.showInputDialog(null,question1);
-
-
JOptionPane.showInputDialog(null,question3);
-
-
-
}
-
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[] questions, String[] answers) {
-
-
int count = 0;
-
int point = 0;
-
-
-
for(int j = 0; j < questions.length; j++) { // i think i have to change the for loop here
-
-
timeForMore = true;
-
String input = JOptionPane.showInputDialog(null, questions[j]); // shows questions in a dialog box together with input line
-
-
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;
-
}
-
-
-
-
try {
-
-
JOptionPane.showMessageDialog(null, "You answered " + count +
-
" out of " + questions.length +
-
" questions correctly.");
-
JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + ((point*100)/10) + " % ");
-
}
-
-
-
class AnswerStore { // storing answers of each quiz in arrays
-
String[] tectonicAnswers = {
-
"Hellenic", "destructive", "100km", "Italy", "Wegner",
-
"Mariana", "Sicily", "created", "constructive", "Mediterranean"
-
};
-
-
String[] riverAnswers = {
-
"Gorges", "Meanders", "Levees", "Yes", "Less Economic Developed Countries",
-
"crescent shaped lakes", "More Economic Developed Countries", "No", "River Discharge", "No"
-
};
-
-
String[] rockAnswers = {
-
"40km", "Igneous Rock", "Sedimentary", "Basalt", "Organic",
-
"pressure", "Oolites", "Igneous", "dark black", "basalt"
-
};
-
-
-
}
-
-
}
-
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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
| | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|