View Single Post
  #6 (permalink)  
Old 12-24-2007, 03:45 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Without seeing the file you are trying to read I have no idea what might be going awry. How your files are written and how you attempt to read them plays a big part in what you get from the readFiles method. I made up a text file with ten lines in it and read it for the plate tectonics quiz. Typing in the answers from the AnswerStore.answers string array worked okay.
Code:
import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import javax.swing.*; public class GQ extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 140; private static final int FRAME_HEIGHT = 160; private static final int FRAME_X_ORIGIN = 70; private static final int FRAME_Y_ORIGIN = 50; AnswerStore answerStore = new AnswerStore(); public static void main (String[] args) { JOptionPane.showMessageDialog(null, "This is a Geography Quiz"); JOptionPane.showMessageDialog(null, "Good Luck"); String passString = JOptionPane.showInputDialog("Enter the Password"); //Password = passString.nextInt(); int Password = Integer.parseInt(passString.trim()); if (Password == 123) { JOptionPane.showMessageDialog(null, "Valid. You typed the right password. " + "Now choose from the following menu"); GQ frame = new GQ(); frame.setVisible(true); } else { JOptionPane.showMessageDialog(null, "Invalid Password. Try Again"); } } public GQ() { Container contentPane; JButton button1, button2, button3, button4, button5; setSize (FRAME_WIDTH, FRAME_HEIGHT); setTitle("Geography Quiz"); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane = getContentPane(); contentPane.setBackground(Color.pink); contentPane.setLayout(new FlowLayout()); 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 void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); String[] questions = null; if (ac.equals("b1")) { questions = readFile("plate_tectonics.txt"); } else if(ac.equals("b2")) { questions = readFile("rivers.txt"); } else if(ac.equals("b3")) { questions = readFile("rocks.txt"); } else if (ac.equals("b4")) { System.exit(0); } askQuestions(questions); } private String[] readFile(String path) { Scanner s = null; StringBuilder sb = new StringBuilder(); String separator = "\n"; try { s = new Scanner(new BufferedReader(new FileReader(path))); // s.useDelimiter(",\\s*"); while (s.hasNext()) { sb.append(s.nextLine() + separator); // JOptionPane.showMessageDialog(null,s.next()); } } catch(IOException e) { System.out.println("read error: " + e.getMessage()); } finally { if (s != null) s.close(); // break; } // What did we get? System.out.println("sb = " + sb.toString()); System.out.printf("sb.split = %s%n", Arrays.toString(sb.toString().split("\\n"))); return sb.toString().split("\\n"); } private void askQuestions(String[] questions) { System.out.printf("questions = %s%n", Arrays.toString(questions)); int count = 0; for(int j = 0; j < questions.length; j++) { String input = JOptionPane.showInputDialog(null, questions[j]); if(answerStore.answers[j].equals(input)) count++; } JOptionPane.showMessageDialog(null, "You answered " + count + " out of " + questions.length + " questions correctly."); } } class AnswerStore { String[] answers = { "Hellenic", "constructive", "100km", "Italy", "Wegner", "constructive", "100km", "Italy", "destroyed", "Mediterranean" }; }
plate_tectonics.txt
Code:
Question 1 Question 2 Question 3 Question 4 Question 5 Question 6 Question 7 Question 8 Question 9 Question 10
Reply With Quote