在Java中将逗号分隔的文件读入多维数组

在Java中将逗号分隔的文件读入多维数组,java,csv,multidimensional-array,Java,Csv,Multidimensional Array,我想用Java将多项选择题列表读入多维数组,文件格式为:问题、答案1、答案2、答案3、答案4、正确答案 一公里有多少米, 彩虹童谣里没有哪种颜色?蓝色,粉色,黑色,橙色,3 一个足球队在场上有多少球员?、10、11、12、13、2 因此,我希望数组的格式为问题[],如果n为1,那么问题[n][1]将是CSV文件中的第一个问题,然后选择一个问题,我可以将n更改为我想要的任何内容 我不知道会有多少问题,它们会不断地从CSV文件中添加或删除,所以不会有静态数量。因此,问题是如何以简单的方式从CSV文件

我想用Java将多项选择题列表读入多维数组,文件格式为:问题、答案1、答案2、答案3、答案4、正确答案

一公里有多少米, 彩虹童谣里没有哪种颜色?蓝色,粉色,黑色,橙色,3 一个足球队在场上有多少球员?、10、11、12、13、2

因此,我希望数组的格式为问题[],如果n为1,那么问题[n][1]将是CSV文件中的第一个问题,然后选择一个问题,我可以将n更改为我想要的任何内容


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

最简单的方法是创建一个
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 questions=new ArrayList();
//创建包含问题的对象。
扫描仪s=新扫描仪(新文件(“文件路径”));
//创建一个扫描器对象以遍历文件中的每一行。
而(s.hasNextLine()){
//而文件中仍存在尚未加载的行。。
添加(s.nextLine().split(“,”);
//加载值数组,在逗号处拆分。
}
最后,您将得到一个
ArrayList
对象,其中每个条目都是一个
String[]
,其长度与每行上的令牌数相同

编辑


如本答案注释中所述,您可以在
ArrayList
类中调用
toArray
方法,以获得多维数组。

您需要设置一个嵌套for循环来处理此问题:

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];
    }
}
for(i=0;i<问题数量;i++)
{
行\数组=当前\输入\行。拆分(“,”)
问题[i]=行数组[0]
对于(j=1;j
当您必须为数据层次结构创建二维数组时,您可能应该为其创建一个合理的模型

这里有一个快速(且不干净)的模型供您使用(因为打字速度而丢弃了setter):

问卷类别:

/**
 * 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;
    }
}
其用途如下:

// 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);
    }
}
//重复第三到第五个问题
对于(int-questionIndex=2;questionIndex<5;questionIndex++){
//获取当前问题
问题=问卷。获取(问题索引);
//重复所有答案
对于(int-answerIndex=0;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.
//创建新的调查问卷
问卷调查=新问卷(“我最棒的问卷”);
//让我们扫描文件并将其加载到调查问卷
Scanner Scanner=新扫描仪(新文件(“myfile.txt”);
//从该文件中读取行并将其拆分为令牌
字符串行,标记[];
整数索引;
while(scanner.hasNextLine()&&(line=scanner.nextLine())!=null){
标记=行分割(“,”);
//以第一个标记为文本创建问题
问题=新问题(标记[0]);
//检查标记,从第一个索引到最后一个索引
for(tokenIndex=1;tokenIndex
您能告诉我们您尝试过什么吗?您能使用第三方库吗?否则你需要解释转义和引用吗?我一直在使用一个简单的CSV阅读教程,但我马上遇到了问题,因为我想要一个多维数组,你甚至不能在不知道它将有多少行的情况下声明其中的一行。显然,你可以在最后调用
toArray
,并得到一个数组。所以我刚才尝试的是String[][]questionsarray=questions.toArray();,但这会带来一个错误,我是否遗漏了一些基本的东西(“类型不匹配:无法从Object[]转换为String[][]”可能是因为
ArrayList
类中有一个用于存储数据的后端数组。问题是后端数组不是
String[][]类型
,所以你从什么中得到了一个例外。你可能只需要坚持使用
数组列表
。感谢你的回答,问题仍然是我必须从某个地方读入问题,最终用户将永远无法访问代码,程序员也不会维护它,因此更改问题的唯一方式是我通过一个外部文件。欣赏你在问卷中的幽默:D.编辑:哦,你好编辑:德威尔,这不仅仅是一个好的做法,它还将使你的生活更轻松。而不是试图解决一个令人难以置信的问题(当然是相对于人而言)算法问题,你可以通过正确地排列数据来解决,并节省时间。尽管看起来需要更多的工作,但事实并非如此。谢谢这看起来很完美,我会将其更改为适合我的程序,但这正是我想要的,非常感谢:)如果你还在,还有一件事,问卷中问题和答案的迭代不起作用。他们肯定会被添加到调查问卷中,我已经打印了通过调查问卷时的问题和答案
// 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.