I want to read a list of multiple choice questions into a multidimensional array in Java, the file is in the format: Question,answer1,answer2,answer3,answer4,correctanswer .
How many meters are in a kilometer?,1,10,100,1000,4, Which colour isn't in the rainbow nursery rhyme?,Blue,Pink,Black,Orange,3 How many players does a football team have on the pitch?,10,11,12,13,2
And so I want the array to be in the format Question[][] where if n was 1 then Question[n][1] would be the first question in the CSV file, then to select a question I can just change n to whatever I want.
I don't know how many questions there will be, they will be continuously added or removed from the CSV file so there isn't going to be a static amount. So the question is how do I load all of the Questions from the CSV file in a simple way?
The simplest approach is create an ArrayList
or arrays. This seems complex, but using the ArrayList
means you don't have to worry about the amount of questions.
ArrayList<String[]> questions = new ArrayList<String[]>();
// Create the object to contain the questions.
Scanner s = new Scanner(new File("path to file"));
// Create a scanner object to go through each line in the file.
while(s.hasNextLine()) {
// While lines still exist inside the file that haven't been loaded..
questions.add(s.nextLine().split(","));
// Load the array of values, splitting at the comma.
}
In the end, you wind up with an ArrayList
object, where each entry is a String[]
with the same length as the number of tokens on each line.
As mentioned in the comments on this answer, you can simply call the toArray
method inside the ArrayList
class, to get a multi-dimensional array.
You will want to set up a nested for loop to handle this:
for(i = 0; i < number_of_questions; i++)
{
line_array = current_input_line.split(",")
Questions[i] = line_array[0]
for(j = 1; j < line_array.length; j++)
{
Questions[i][j] = line_array[j];
}
}
When you get to a point where you have to make a two-dimensional array for data hierarchy, you should probably create a sensible model for it.
Here is a quick (and dirty) model for you (setters discarded for typing speed):
Questionnaire class:
/**
* Facilitates an entire questionnaire
*/
public class Questionnaire extends ArrayList<Question> {
/**
* This questionnaire's name
*/
private String name;
/**
* Creates a new questionnaire using the specified name
* @param name The name of this questionnaire
*/
public Questionnaire(String name) {
this.name = name;
}
/**
* Returns the name of this questionnaire
*/
public String getName() {
return name;
}
}
Question class:
/**
* Facilitates a question and its answers
*/
public class Question extends ArrayList<Answer> {
/**
* The question's text
*/
private String text;
/**
* Constructs a new question using the specified text
* @param text The question's text
*/
public Question(String text) {
this.text = test;
}
/**
* Returns this question's text
*/
public String getText() {
return text;
}
}
Answer class:
/**
* Facilitates an answer
*/
public class Answer {
/**
* The answer's text
*/
private String text;
/**
* Whether or not this answer is correct
*/
private boolean correct;
/**
* Constructs a new answer using the specified settings
* @param text The text of this answer
* @param correct Whether or not this answer is correct
*/
public Answer(String text, boolean correct) {
this.text = text;
this.correct = correct;
}
/**
* Returns this answer's text
*/
public String getText() {
return text;
}
/**
* Whether or not this answer is correct
*/
public boolean isCorrect() {
return correct;
}
}
Usage for it would be as follows:
// Create a new questionnaire
Questionnaire questionnaire = new Questionnaire("The awesome questionnaire");
// Create a question and add answers to it
Question question = new Question("How awesome is this questionnaire?");
question.add(new Answer("It's pretty awesome", false));
question.add(new Answer("It's really awesome", false));
question.add(new Answer("It's so awesome my mind blows off!", true));
// Add the question to the questionnaire
questionnaire.add(question);
Iterating over it is pretty easy:
// Iterate over the questions within the questionnaire
for(Question question : questionnaire) {
// Print the question's text
System.out.println(question.getText());
// Go over each answer in this question
for(Answer answer : question) {
// Print the question's text
System.out.println(answer.getText());
}
}
You could also iterate just a part of it:
// Iterate over the third to fifth question
for (int questionIndex = 2; questionIndex < 5; questionIndex ++) {
// Get the current question
Question question = questionnaire.get(questionIndex);
// Iterate over all of the answers
for (int answerIndex = 0; answerIndex < question.size(); answerIndex++) {
// Get the current answer
Answer answer = question.get(answerIndex);
}
}
Reading a file into the model using the format you described can be done the following way:
// Create a new questionnaire
Questionnaire questionnaire = new Questionnaire("My awesome questionnaire");
// Lets scan the file and load it up to the questionnaire
Scanner scanner = new Scanner(new File("myfile.txt"));
// Read lines from that file and split it into tokens
String line, tokens[];
int tokenIndex;
while (scanner.hasNextLine() && (line = scanner.nextLine()) != null) {
tokens = line.split(",");
// Create the question taking the first token as its text
Question question = new Question(tokens[0]);
// Go over the tokens, from the first index to the one before last
for (tokenIndex = 1; tokenIndex < tokens.length-1; tokenIndex++) {
// Add the incorrect answer to the question
question.add(new Answer(tokens[tokenIndex], false));
}
// Add the last question (correct one)
question.add(new Answer(tokens[tokenIndex],true));
}
// Tada! questionnaire is now a complete questionnaire.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.