Results 1 to 2 of 2
- 01-16-2008, 04:22 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 34
- Rep Power
- 0
reading textfile from java problem
I have this problem, where i am saving the answers of a set of questions in an array. The questions are stored in a textfile. However i wish to store the answers also in a textfile instead of in an array, but my problem is how am i going to compare these answers stored in the textfile to the answer enterd by the user?
This is the code:
Java 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.*; 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; 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); 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); } 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; private String[] readFile(String path) { Scanner s = null; StringBuilder sb = new StringBuilder(); String separator = "\n"; try { s = new Scanner(new BufferedReader(new FileReader(path))); while (s.hasNextLine()) { sb.append(s.nextLine() + separator); } } catch(IOException e) { System.out.println("read error: " + e.getMessage()); } finally { if (s != null) s.close(); // or break } 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++) { 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; } JOptionPane.showMessageDialog(null, "You answered " + count + " out of " + questions.length + " questions correctly."); JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + ((point*100)/10) + " % "); } 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"); } } public static String getSpace(int length){ String str = ""; for(int i = 0 ; i < length ; i++ ){ str = str + " " ; } return str ; } class AnswerStore { // storing answers of each quiz in arrays String[] tectonicAnswers = { "Hellenic", "destructive", "100km", "Italy", "Wegner", "Mariana", "Sicily", "created", "constructive", "Mediterranean", "jjj", "jjj", "jjj" }; 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", "", "", "" }; } }Last edited by saytri; 01-16-2008 at 08:47 PM.
- 01-17-2008, 02:13 AM #2
Another user had a similar problem, so check my reply
Other Thread
(I hope this isn't useless to you =])
Similar Threads
-
ECG plot in a textfile
By samson in forum Java 2DReplies: 13Last Post: 01-28-2012, 04:02 PM -
Search TextFile
By gsupriyarao@yahoo.com in forum Advanced JavaReplies: 5Last Post: 04-11-2008, 11:03 AM -
textfile with Java problems
By saytri in forum New To JavaReplies: 4Last Post: 12-29-2007, 05:16 PM -
Problem reading an xml file with AJAX
By Fiona80 in forum XMLReplies: 0Last Post: 12-17-2007, 08:02 PM -
Problem with storing and retrieving from a textfile
By Albert in forum Advanced JavaReplies: 1Last Post: 07-13-2007, 03:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks