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 12-29-2007, 10:33 AM
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
textfile with Java problems
I am making a quiz, and i wrote the questions in a textfile. But i wish that the questions when they are called from Java come up randomly, not always in the same state. What can i do? Thanks a lot for the help.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-29-2007, 10:46 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Using Math.random() and text files.
Hi!. Here's a method that will read a text file:
Code:
public static Vector<String> loadFile(String filename){ Vector<String> strings = new Vector<String>(); try{ FileReader file = new FileReader(filename); BufferedReader buffer = new BufferedReader(file); while (true) { String line = buffer.readLine(); if (line == null) break; else { strings.add(line); } } buffer.close(); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } return strings; }
Using this method and Math.random(), you can access random lines in your text file. Lets assume that each question is on a separate line in the text file. Your main() method could look like this.
Code:
public static void main(String[] arguments){ Vector<String> data = loadFile("questions.txt"); int pos = (int)((double)data.size() * Math.random()); System.out.println("Next question: " + data.get(pos)); }
Remember to add the imports:
Code:
import java.io.*; import java.util.*;
My code has been checked by hand, so just scream if there is a problem.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-29-2007, 11:00 AM
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
Thanks a lot for your help. :-) But i have already written the piece of code where i read the file, and since its a bit differnet from yours, i got a bit confused oh how to implement the Math.random() into my piece of code. Can you pls help me do the method Math.random into my piece of code, because i got confused. Thanks a lot. I appreciate a lot your help. :-)

This is the code i have written:

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"); GQ frame = new GQ(); frame.setVisible(true); } 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; String[] answers = null; if (ac.equals("b1")) { questions = readFile("plate_tectonics.txt"); answers = answerStore.tectonicAnswers; } else if(ac.equals("b2")) { questions = readFile("rivers.txt"); answers = answerStore.riverAnswers; } else if(ac.equals("b3")) { questions = readFile("rocks.txt"); answers = answerStore.rockAnswers; } else if (ac.equals("b4")) { System.exit(0); } askQuestions(questions, answers); } 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; } 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, String[] answers) { System.out.printf("questions = %s%nanswers = %s%n", Arrays.toString(questions), Arrays.toString(answers)); int count = 0; int point = 0; for(int j = 0; j < questions.length; j++) { String input = JOptionPane.showInputDialog(null, questions[j]); if(answers[j].equals(input)) { count++; point++; } } 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"); } } } class AnswerStore { 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" }; }
Thanks again
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-29-2007, 11:34 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Okay
Let's create a mix method:
Code:
public static String[] mix(String[] array){ Vector<String> data = new Vector<String>(); String[] result = new String[array.length]; for (String string : array) data.add(string); int count = 0; while (data.size() > 0){ int pos = (int)(Math.random() * (double)data.size()); String next = data.get(pos); result[count++] = next; data.remove(pos); } return result; }
Use this method to mix the array before you use it:
Code:
questions = mix(questions);
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-29-2007, 06:16 PM
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
Ok thanks a lot. Happy New Year! :-)
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
reading textfile from java problem saytri New To Java 1 01-17-2008 03:13 AM
Textfile and GUI problems saytri New To Java 2 12-21-2007 05:08 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 05:17 PM.


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