Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-16-2008, 05:22 PM
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
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:

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 09:47 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-17-2008, 03:13 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Another user had a similar problem, so check my reply

Other Thread

(I hope this isn't useless to you =])
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search TextFile gsupriyarao@yahoo.com Advanced Java 5 04-11-2008 12:03 PM
textfile with Java problems saytri New To Java 4 12-29-2007 06:16 PM
Problem reading an xml file with AJAX Fiona80 XML 0 12-17-2007 09:02 PM
ECG plot in a textfile samson Java 2D 1 07-17-2007 04:53 AM
Problem with storing and retrieving from a textfile Albert Advanced Java 1 07-13-2007 04:01 PM


All times are GMT +3. The time now is 11:12 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org