2013-04-07 86 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]; 
    } 
} 
相關問題