This site requires JavaScript, please enable it in your browser!
Greenfoot back
Omniscience
Omniscience wrote ...

2012/3/24

Reading and Writing to File

1
2
matt.milan matt.milan

2012/3/25

#
what would be the advantages (or limitations) of using a 2d array instead of multiple 1d arrays in this instance
Duta Duta

2012/3/25

#
You could have a 2d array for the 4 possible answers for ease of iterating over the possible answers.
danpost danpost

2012/3/25

#
It is easier to write code for (especially because all fields are of String type). If you noticed, the same code parsed all the fields, instead of having to write out the parsing of each field individually. Also, if the data needed to be sent to a method, only one parameter of type String need be sent if sending all the data (if just sending the data for one question, then one parameter of type String). Example (using data): If sending all data: methodName(data) If sending data for just one question: methodName(data) You could also have 'int final's in your code: int final QUESTION = 0; int final ANS_A = 1; int final ANS_B = 2; int final ANS_C = 3; int final ANS_D = 4; int final ANSWER = 5; int final RESPONSE = 6; int final JUDGEMENT = 7; and use them in your code to make it more readable, as: data
danpost danpost

2012/3/25

#
I thought I had better re-post my code, as I found that 'field' had to be declared outside the for loop also.
int maxQuestions = 3;
int questionCount;
String[][] data = new String[maxQuestions][8];
// above outside method -- below inside method
int count, field;
for (count = 0; count < maxQuestions; count++)
{
    line = in.readLine();
    if (line == null) break;
    for (field = 0; field < 7; field++)
    {
        if (line.indexOf(',') < 0) break;
        data[count][field] = line.substring(0, line.indexOf(','));
        line = line.substring(line.indexOf(',') + 1);
    }
    data[count][field] = line;
}
questionCount = count;
Omniscience Omniscience

2012/3/26

#
@everybody ..... woah. Okay, umm, this is a lot for me to take in, a lot of stuff like arrayLists and subStrings that I've never used or heard of before. It's especially hard for me, seeing as I only take in what I understand. I've understood BufferedReader, and how to sort the contents into a single array, but the rest is beyond me. Thankyou though, for all this helpful feedback! @duta I've never used an arrayList before... if you'd be so kind as to give me the outline of what's happening through your code? Thanks! @danpost your code seems the simplest to understand. Will you please give me a breakdown of how your code is working?
Duta Duta

2012/3/26

#
Well what my code is effectively doing is going through the file, reading it one line at a time (and saving the read line into the String line, and then organizing it into List's (which are similar to arrays - a List is what it says on the tin: A list of objects, in this case String objects, which we stated when creating the lists through calling them a List<String>). I've since realized that my code is stupid - here's a better version:
/*
 * I assume you've created your BufferedReader and called it "in"
 * This also instead of just reading 3 questions, it reads however
 * many questions there are in the file.
 */
import java.util.List;
import java.util.ArrayList;

List<String>
	question = new ArrayList<String>(),
	answer_a = new ArrayList<String>(),
	answer_b = new ArrayList<String>(),
	answer_c = new ArrayList<String>(),
	answer_d = new ArrayList<String>(),
	answer_correct = new ArrayList<String>();
String line;
while((line = in.readLine()) != null)
{ try {
	String[] data = line.split(",");
	question.add(data[0]);
	answer_a.add(data[1]);
	answer_b.add(data[2]);
	answer_c.add(data[3]);
	answer_d.add(data[4]);
	answer_correct.add(data[5]);
} catch(Exception e) {}}
//If you really need it in arrays:
String[]
	q = question.toArray(),
	a = answer_a.toArray(),
	//etc
I will give a walkthrough later, but I've got to go now! Sorry, I hope you understand it
danpost danpost

2012/3/26

#
// the following variables are instance variables
// they are declared in the class but outside of any methods
int maxQuestions = 3; //maximum possible number of questions
int questionCount; // the final total number of questions read in
String[][] data = new String[maxQuestions][8];
// the 2D array of Strings will hold all the data read in
// the number inside the first [ ] is the question number
// the number inside the second [ ] is the field number
// The following code goes inside of a method
int count, field; // since these will continue to be used outside the for loop, they are declared first
// (the 'int field;' could be declared before the inside for loop)
for (count = 0; count < maxQuestions; count++) // read in the file, one line at a time
{
    line = in.readLine(); // reads a Line into 'line'
    if (line == null) break; // if no lines left exit for loop
    for (field = 0; field < 7; field++) // break the line down, one field at a time
    {
        if (line.indexOf(',') < 0) break; // if no seperators left, exit for loop (only one field left)
        data[count][field] = line.substring(0, line.indexOf(',')); // save first field of line
        line = line.substring(line.indexOf(',') + 1); // remove first field from line
    }
    data[count][field] = line; // save last field of line
}
questionCount = count; // save total questions read in
danpost danpost

2012/3/26

#
To learn about 'a lot of stuff' (or just substrings) go to the java language tutorials. Select 'Numbers and String' then 'Strings' or 'Manipulating Strings'.
You need to login to post a reply.
1
2