Results 1 to 5 of 5
- 10-28-2010, 02:44 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
Array, I/O with a .txt file problems
Okay now i have a .txt file thats called questions... and it looks similar to this
5 // 5 is for the number of questions
How many different three-member teams can be formed from six students?
4 // number of answers
20
120
216
720
1 // the answer
0 // the number of times the q was answered
0 // the number of times the q was answered correctly
Which Steeler was named Super Bowl IX MVP?
3
Terry Bradshaw
Lynn Swann
Franco Harris
3
0
0
as you see thats just 2 questions... i need to have each question display one at a time... i have no idea how to put this into an array... here is my code:
Java Code:public String getQuestion (Question[] array, String qq) { try { x = new Scanner(new File("questions.txt")); } catch(Exception e) { System.out.println("could not find file"); } for(int index = 0; index <array.length; index++) { while (x.hasNextLine() && index < array.length) { //array[index] = x.nextLine(); String a = x.nextLine(); System.out.printf("%s\n", a); } } x.close(); return question; }
Java Code:public class Assig3 { public static void main (String [] args) throws IOException { String questions1 = ""; final int SIZE = 5; Question [] allQuestions = new Question [SIZE]; Question q = new Question (); }
- 10-28-2010, 06:45 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 785
- Rep Power
- 12
How does your Question class look like?
>>"i have no idea how to put this into an array"
first, read the first line / integer. after that you know how many times you have to iterate -> write a loop from 0 to the number of questions and create a question array of that size.
in the loop read the next two lines. store the first in a string (question), the second in an integer. create an array(answers) with the size of that integer.
then you have to write another "nested" loop from 0 to the number of answers/arrays length and read next lines and store them into the array.
read a next line again to get the right answer(rightAnswer)
after that, create a question instance e.g. new Question(question,answers,rightAnswer);
and store it to the first created "question" array.
thats all :)
maybe you should reconsider the file. perhaps xml or json would be better and easier to handle?!Last edited by eRaaaa; 10-28-2010 at 07:12 AM.
- 10-28-2010, 09:24 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
hey thanks i will give it a try.. i have tho created an array of SIZE 5 and sent that to my Question.java class in the getQuestion method... but i just didn't what to do since the integer 5 was the first line of my questions.txt document..how to go through each question.. Here is my Question class:
Java Code:import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; // class represents the questions public class Question { private int correctAnswer; // needed private int numberTried; // needed private int numberCorrect; // needed private String question; Question [] answers; // array of strings for answers private Scanner x; public Question() // constructor...size array of answers { } public String getQuestion (int[] allQuestions, String qq) { try { x = new Scanner(new File("questions.txt")); // brings in file assigns file to x } catch(Exception e) { System.out.println("could not find file"); } for(int index = 0; index <allQuestions.length; index++) { while (x.hasNextLine() && index < allQuestions.length) { //array[index] = x.nextLine(); String a = x.nextLine(); System.out.printf("%s\n", a); } } x.close(); return question; } public int getAnswer() { return correctAnswer; } public int getTries() { return numberTried; } public int getCorrect() { return numberCorrect; } } // end Question class this program will execute but whenever it does all it does is display my WHOLE questions.txt document
- 10-28-2010, 10:11 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
Ok so i have modified it and i have got to here:
Java Code:public class Question { private int correctAnswer; // needed private int numberTried; // needed private int numberCorrect; // needed private String question; // array of strings for answers public Question() // constructor...size array of answers { } public String getQuestion (int[] allQuestions, String qq, String answer) throws IOException { int index = 0; question = qq; File file = new File ("questions.txt"); Scanner inputFile = new Scanner (file); int numQ = inputFile.nextInt(); for (index = 0; index < allQuestions.length; index++) { question = inputFile.nextLine(); System.out.print(question); } return question; } }
This is the output: How many different three-member teams can be formed from six students?420120
which i need to just show the answers underneath...
any ideasLast edited by basketball8533; 10-28-2010 at 10:34 AM.
- 10-28-2010, 10:49 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 55
- Rep Power
- 0
Usually when you have a small problem like this a temporary workaround is usually the answer for me, although this solution is probably going to be frowned upon by others:
if it's only a single digit number:
Java Code:// Remove the extra number at the beginning question = question.substring(1);
Java Code:try{ for(int i = 0; i < question.length(); i++){ Integer.parseInt(question.substring(0,1)); question = question.substring(1); } } catch(Exception e){ return question; } return question;
Similar Threads
-
Array problems
By braddy in forum New To JavaReplies: 4Last Post: 10-09-2010, 02:18 PM -
Problems With Array/Methods
By blueduiker in forum New To JavaReplies: 4Last Post: 01-19-2010, 02:49 AM -
Array problems!
By Addez in forum New To JavaReplies: 4Last Post: 08-29-2009, 07:56 PM -
Array problems..
By smokeviolent in forum New To JavaReplies: 1Last Post: 04-17-2009, 07:45 AM -
Array problems
By Hosticus in forum New To JavaReplies: 2Last Post: 01-18-2009, 03:48 AM
Bookmarks