View Single Post
  #7 (permalink)  
Old 01-20-2008, 09:18 PM
mcal mcal is offline
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
Oh that was so helpful. I really am thankful. But i can't understand why the counter still isn't incremented. It seems when i call this:

Code:
askQuestions(questions, answers);
it just skips the for loop and continues displaying the others:

Code:
JOptionPane.showMessageDialog(null, "You answered " + count + " out of " + questions.length + " questions correctly."); int percent = (count*100)/questions.length; JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + percent + " % ");
It is just showing: "You answered 0 out of 1 questions correctly". How come it is saying one question when i have 10 questions? I really got confused. Thanks a lot for your help. You are really kind, and i really appreciate your help. When i was working with the textfile, the program worked fine, but now (with the binary file) it isn't.

This is the whole code, where i arranged what you told me:

Code:
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.FileInputStream; import java.io.FileOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.EOFException; 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; private JMenu OptionMenu; static final String dataFile1 = "plate_tectonics.dat"; static final String dataFile2 = "rivers.dat"; static final String dataFile3 = "rocks.dat"; 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(); createOptionMenu(); JMenuBar menuBar= new JMenuBar(); setJMenuBar(menuBar); menuBar.add(fileMenu); menuBar.add(HelpMenu); menuBar.add(OptionMenu); 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"); button1.setBounds(350,225,300,50); button2.setBounds(350,325,300,50); button3.setBounds(350,425,300,50); button4.setBounds(350,525,300,50); button1.setFont(new Font("Arial", Font.BOLD, 30)); button2.setFont(new Font("Arial", Font.BOLD, 30)); button3.setFont(new Font("Arial", Font.BOLD, 30)); button4.setFont(new Font("Arial", Font.BOLD, 30)); button1.setBorder(BorderFactory.createRaisedBevelBorder()); button2.setBorder(BorderFactory.createRaisedBevelBorder()); button3.setBorder(BorderFactory.createRaisedBevelBorder()); button4.setBorder(BorderFactory.createRaisedBevelBorder()); button1.setBackground (Color.pink); button1.setForeground (Color.blue); button2.setBackground (Color.pink); button2.setForeground (Color.blue); button3.setBackground (Color.pink); button3.setForeground (Color.blue); button4.setBackground (Color.pink); button4.setForeground (Color.blue); prompt = new JLabel( ); prompt.setText("Geography Quiz"); prompt.setSize(900,50); prompt.setLocation(350,30); prompt.setFont(new Font("Times New Roman", Font.BOLD, 40)); prompt.setForeground(Color.red); contentPane.add(prompt); prompt = new JLabel( ); prompt.setText("Choose any of the following:"); prompt.setSize(900,80); prompt.setLocation(325,100); prompt.setFont(new Font("Times New Roman", Font.BOLD, 30)); contentPane.add(prompt); 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); setDefaultCloseOperation(EXIT_ON_CLOSE); contentPane.setBackground(Color.green); contentPane.setLayout(new BorderLayout()); Test4Rx imagePanel = new Test4Rx("geo2.jpg"); contentPane.add(imagePanel, "North"); contentPane.add(new Test4Rx("geo2.jpg")); pack(); setLocationRelativeTo(null); setVisible(true); } 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(); menuName = event.getActionCommand(); //Action performed by 'Quit' menu item. if(menuName.equals("Quit")){ System.exit(0); } if(menuName.equals("About Quiz")){ JOptionPane.showMessageDialog(null, " This is a Geography Quiz. " + " You can choose from 3 different quizzes. " + " Questions should be answered within 50 seconds. "); } if(menuName.equals("Add a Plate Tectonic Question")){ final JPF2 appRef = new JPF2(); final JFrame frame = new JFrame("Password"); JLabel jlbPassword = new JLabel("Enter the password: "); JPasswordField jpwName = new JPasswordField(10); jpwName.setEchoChar('*'); jpwName.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JPasswordField input = (JPasswordField)e.getSource(); char[] password = input.getPassword(); if (isPasswordCorrect(password)) { JOptionPane.showMessageDialog(frame, "Correct password."); try { String question = JOptionPane.showInputDialog(null, "Enter new question"); File aFile = new File( "plate_tectonics.dat" ); // create an output stream to the file FileOutputStream aFileOutStream = new FileOutputStream ( aFile ); // create a data output stream to the file output stream DataOutputStream aDataOutStream = new DataOutputStream ( aFileOutStream ); // write data to file aDataOutStream.writeUTF( question ); aFileOutStream.close(); }catch(IOException event){ System.out.println("There was a problem:" + e); } } else { JOptionPane.showMessageDialog(frame, "Sorry. Try again.", "Error Message", JOptionPane.ERROR_MESSAGE); } } }); JPanel jplContentPane = new JPanel(new BorderLayout()); jplContentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); jplContentPane.add(jlbPassword, BorderLayout.WEST); jplContentPane.add(jpwName, BorderLayout.CENTER); frame.setContentPane(jplContentPane); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.pack(); frame.setVisible(true); } if(menuName.equals("Add a River Question")){ final JPF2 appRef = new JPF2(); final JFrame frame = new JFrame("Password"); JLabel jlbPassword = new JLabel("Enter the password: "); JPasswordField jpwName = new JPasswordField(10); jpwName.setEchoChar('*'); jpwName.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JPasswordField input = (JPasswordField)e.getSource(); char[] password = input.getPassword(); if (isPasswordCorrect(password)) { JOptionPane.showMessageDialog(frame, "Correct password."); try { String question = JOptionPane.showInputDialog(null, "Enter new question"); File aFile = new File( "rivers.dat" ); // create an output stream to the file FileOutputStream aFileOutStream = new FileOutputStream ( aFile ); // create a data output stream to the file output stream DataOutputStream aDataOutStream = new DataOutputStream ( aFileOutStream ); // write data to file aDataOutStream.writeUTF( question ); aFileOutStream.close(); }catch(IOException event){ System.out.println("There was a problem:" + e); } } else { JOptionPane.showMessageDialog(frame, "Sorry. Try again.", "Error Message", JOptionPane.ERROR_MESSAGE); } } }); JPanel jplContentPane = new JPanel(new BorderLayout()); jplContentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); jplContentPane.add(jlbPassword, BorderLayout.WEST); jplContentPane.add(jpwName, BorderLayout.CENTER); frame.setContentPane(jplContentPane); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.pack(); frame.setVisible(true); } if(menuName.equals("Add a Rocks Question")){ final JPF2 appRef = new JPF2(); final JFrame frame = new JFrame("Password"); JLabel jlbPassword = new JLabel("Enter the password: "); JPasswordField jpwName = new JPasswordField(10); jpwName.setEchoChar('*'); jpwName.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JPasswordField input = (JPasswordField)e.getSource(); char[] password = input.getPassword(); if (isPasswordCorrect(password)) { JOptionPane.showMessageDialog(frame, "Correct password."); try { String question = JOptionPane.showInputDialog(null, "Enter new question"); File aFile = new File( "rocks.dat" ); // create an output stream to the file FileOutputStream aFileOutStream = new FileOutputStream ( aFile ); // create a data output stream to the file output stream DataOutputStream aDataOutStream = new DataOutputStream ( aFileOutStream ); // write data to file aDataOutStream.writeUTF( question ); aFileOutStream.close(); }catch(IOException event){ System.out.println("There was a problem:" + e); } } else { JOptionPane.showMessageDialog(frame, "Sorry. Try again.", "Error Message", JOptionPane.ERROR_MESSAGE); } } }); JPanel jplContentPane = new JPanel(new BorderLayout()); jplContentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); jplContentPane.add(jlbPassword, BorderLayout.WEST); jplContentPane.add(jpwName, BorderLayout.CENTER); frame.setContentPane(jplContentPane); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.pack(); frame.setVisible(true); } String ac = event.getActionCommand(); String[] questions = null; String[] answers = null; if (ac.equals("b1")) { 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); questions = readFile1("plate_tectonics.dat"); answers = answerStore.tectonicAnswers; askQuestions(questions, answers); } else 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); questions = readFile2("rivers.dat"); answers = answerStore.riverAnswers; askQuestions(questions, answers); } else if(ac.equals("b3")) { 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); questions = readFile3("rocks.dat"); answers = answerStore.rockAnswers; askQuestions(questions, answers); } else if (ac.equals("b4")) { System.exit(0); } // calls the questions from textfile } private void createFileMenu(){ // creating a File Menu JMenuItem item; fileMenu = new JMenu("File"); item = new JMenuItem("Quit"); //Quit item.addActionListener(this); fileMenu.add(item); } //Create Edit Menu private void createHelpMenu(){ JMenuItem item; HelpMenu=new JMenu("Help"); item = new JMenuItem("About Quiz"); item.addActionListener(this); HelpMenu.add(item); item = new JMenuItem("Help about Quiz"); item.addActionListener(this); HelpMenu.add(item); } private void createOptionMenu(){ JMenuItem item; OptionMenu=new JMenu("Option"); item = new JMenuItem("Add a Plate Tectonic Question"); item.addActionListener(this); OptionMenu.add(item); item = new JMenuItem("Add a River Question"); item.addActionListener(this); OptionMenu.add(item); item = new JMenuItem("Add a Rocks Question"); item.addActionListener(this); OptionMenu.add(item); } public void stopAndShowResults() { timeForMore = false; // used for countdown. } private String[] readFile1(String path) { StringBuilder sb = new StringBuilder(); String separator = "\n"; String question; DataInputStream in = null; try { in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile1))); String desc; try { while (true) { desc = in.readUTF(); JOptionPane.showInputDialog(null, desc); } } catch (EOFException e) { } } catch(IOException e) { System.out.println( e.getMessage() ); System.exit(1); } return sb.toString().split("\\n"); } private String[] readFile2(String path) { StringBuilder sb = new StringBuilder(); String separator = "\n"; String question; DataInputStream in = null; try { in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile2))); String desc; try { while (true) { desc = in.readUTF(); JOptionPane.showInputDialog(null, desc); } } catch (EOFException e) { } } catch(IOException e) { System.out.println( e.getMessage() ); System.exit(1); } return sb.toString().split("\\n"); } private String[] readFile3(String path) { String question; DataInputStream in = null; try { in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile3))); String desc; try { while (true) { desc = in.readUTF(); JOptionPane.showInputDialog(null, desc); } } catch (EOFException e) { } } catch(IOException e) { System.out.println( e.getMessage() ); System.exit(1); } } public void askQuestions(String[] questions, String[] answers) { int count = 0; int point = 0; Random generator = new Random(); for(int j = 0; j < questions.length; j++) { 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."); int percent = (count*100)/questions.length; JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + percent + " % "); try { BufferedWriter out; String name = JOptionPane.showInputDialog(null, "Enter your name"); String type = JOptionPane.showInputDialog(null, "Enter your quiz type"); out = new BufferedWriter(new FileWriter("players.txt",true)); out.write(name); //Writing to the textfile (the name entered by user) out.write(getSpace(20- name.length())); // Here 20 is max length possible change accordingly String trimmedStr = name.trim(); out.write(String.valueOf(percent+ " % ")); out.write(" "); out.write(type); out.newLine(); //write a new line to the textfile so the next time it writes to the file it does it on the next line out.close(); }catch(IOException e){ System.out.println("There was a problem:" + e); } if(point>=0 && point<=3) { JOptionPane.showMessageDialog(null, "You need to Improve"); } if(point>=4 && point<=7) { JOptionPane.showMessageDialog(null, "Good"); } if(point>=8 && point<=10) { JOptionPane.showMessageDialog(null, "You did Great"); } ReadDataFileToPanel scorelist = new ReadDataFileToPanel(); // calling class to view the score list } public static String getSpace(int length){ String str = ""; for(int i = 0 ; i < length ; i++ ){ str = str + " " ; } return str ; } private static boolean isPasswordCorrect(char[] inputPassword) { char[] actualPassword = { 'h', 'e', 'l', 'l' }; if (inputPassword.length != actualPassword.length) return false; //Return false if lengths are unequal for (int i = 0; i < inputPassword.length; i ++) if (inputPassword[i] != actualPassword[i]) return false; return true; } 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" }; } }

Thnaks again. :-)

Last edited by mcal : 01-20-2008 at 09:21 PM.
Reply With Quote