2013-04-07 84 views
0

我想将多选题的列表读入Java中的多维数组,该文件的格式如下:Question,answer1,answer2,answer3,answer4,correctanswer将逗号分隔的文件读取到Java中的多维数组中

多少米都是在一公里?,1,10,100,1000,4, 哪种颜色是不是在彩虹的童谣?,蓝,粉红,黑,橙,3名 多少玩家呢一支足球队有在球场上?,10,11,12,13,2

所以,我想在阵列是格式问题[] [],其中如果n为1,则问题[N] [1]将是CSV文件中的第一个问题,然后选择一个问题,我可以将n更改为任何我想要的。

我不知道会有多少问题,它们会不断添加或从CSV文件中删除,因此不会有静态数量。所以问题是如何以简单的方式加载CSV文件中的所有问题?

+3

你能告诉我们你试过了什么吗? – Maroun 2013-04-07 18:03:23

+0

你可以使用第三方库吗?否则,你是否需要考虑转义和引用? – 2013-04-07 18:05:33

+0

我一直在[link](http://beginwithjava.blogspot.co.uk/2011/05/java-csv-file-reader.html)使用一个简单的CSV阅读教程,但我立即遇到问题,因为我想要一个多维数组,并且你甚至不知道它会有多少行就可以声明其中的一个。 – Tairi 2013-04-07 18:07:58

回答

0

当你到那里你必须做出对数据层次二维阵列中的点,你应该建立一个明智的模式。

这里是你(丢弃的打字速度制定者),快速(和肮脏的)模型:

问卷类:

/** 
* 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; 
    } 
} 

问题类:

/** 
* 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; 
    } 
} 

回答类:

/** 
* 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; 
    } 
} 

使用它会是如下:

// 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); 

遍历它是非常容易的:

// 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()); 
    } 
} 

你也可以遍历只是其中的一部分:

// 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); 
    } 
} 

读取文件使用您所描述的格式进入模型可以通过以下方式完成:

// 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. 
+0

感谢你的回答,问题仍然是我必须阅读从最终用户的问题出发,终端用户永远不会访问代码,程序员也不会维护它,因此问题变化的唯一方式就是通过外部文件。在问卷中欣赏你的幽默:D。编辑:哦,你好编辑:D – Tairi 2013-04-07 18:44:36

+0

嗯,这不仅仅是好的做法,它也会让你的生活更轻松,而不是试图解决令人惊讶的问题(相对于人当然)算法问题,你可以通过正确地布置您的数据并节省时间来减轻压力。尽管看起来更多的工作,但实际上并非如此。 – 2013-04-07 18:46:50

+0

感谢这看起来很完美,我会改变它适合我的程序,但这正是我一直在寻找的,谢谢一堆:) – Tairi 2013-04-07 18:47:56

0

最简单的方法是创建一个ArrayList或数组。这似乎很复杂,但使用ArrayList意味着您不必担心问题的数量。

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. 
} 

最后,你风与ArrayList对象,其中每个条目是String[]具有相同的长度,每行的令牌的数量。

编辑

正如在这个答案的评论中提到,你可以简单地调用toArray方法ArrayList类中,得到一个多维数组。

+0

我会试一试,然后克里斯,谢谢! – Tairi 2013-04-07 18:11:03

+0

显然你可以在最后调用'toArray'并有一个数组。 – 2013-04-07 18:11:07

+0

你当然可以!我一定会编辑它。 – christopher 2013-04-07 18:11:34

0

您将要建立一个嵌套的循环来处理这个问题:

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]; 
    } 
} 
相关问题