当前位置: 首页 > 知识库问答 >
问题:

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

徐昊焜
2023-03-14

我想把一个多项选择题列表读成一个多维数组在Java,文件的格式是:问题,answer1,answer2,answer3,answer4,CorretAnswer。

一公里有几米?,1,10,100,1000,4,彩虹童谣里哪种颜色不是?,蓝色,粉色,黑色,橙色,3一个足球队在场上有几个队员?,10,11,12,13,2

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

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

共有1个答案

文心思
2023-03-14

当您到了必须为数据层次结构创建二维数组的地步时,您可能应该为其创建一个明智的模型。

这里有一个快速(和脏)的模型为您(setters丢弃为键入速度):

问卷类别:

/**
 * 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.
 类似资料:
  • 问题内容: 当我的输入为1 2 3(以空格分隔)时,该程序运行良好。但是,当我的输入为1,2,3(以逗号分隔)时,如何修改程序。 问题答案: 您可以使用nextLine方法读取字符串,并使用拆分方法将其以逗号分隔,如下所示: 此方法适用于仅用逗号分隔的3个值。 如果需要更改值的数量,可以使用循环从向量中获取值。

  • 目标:创建一个面向对象的图形Java应用程序,该程序将:读取一个CSV(逗号分隔值)文件,该文件由学生姓名(名字、姓氏)、ID以及内容和交付的初始标记组成(未评估学生使用-1值)。 这是我的代码,但当我点击选择文件。。当它真的应该打开文件并读取数据时,它会显示“预期的名字、姓氏、ID、内容和交付”。但不知何故,它不起作用。在此处输入图像描述 下面是我的代码: 私有类ChooseFileListen

  • 我有一个txt文件,数据如下所示 我在使用这段代码时读到了数据: 由于我的时间列,它不能正常工作,因为是通过逗号分隔的。我该如何解决这一点,如何使它工作,即使在我有多列这样的时间格式的情况下? 我想获得一个如下所示的数据帧: 多谢!

  • 如果内容相同,我想将两个文件合并为一个: first.txt second.txt 后果txt 首先,我想读取文件的内容并将其保存到数组中。但是怎么做呢? 输出: 1,2,3 例如,如何将逗号分隔开来,以便我可以编写?

  • 问题内容: 我有一个逗号分隔的CSV文件,其中包含NASDAQ符号。我使用扫描仪读取文件 我在第二个字段上遇到异常。问题是,该字段与文件中的其他某些字段一样也包含逗号,例如“ 1-800 FLOWERS.COM,Inc.”: 如何避免这个问题?我的代码是: 谢谢 问题答案: 除非这是家庭作业,否则您不应该自己解析CSV。使用现有库之一。例如: http //commons.apache.org/s

  • 问题内容: 我们已经知道可以在多个if /guard语句中使用多个可选绑定,方法是用逗号分隔,但不能使用例如 在操场上玩耍时,逗号式格式似乎也适用于布尔条件,尽管我在任何地方都找不到。例如 这是用于评估多个布尔条件的公认方法吗,其行为是否与boolean条件相同或在技术上有所不同? 问题答案: 实际上结果是不一样的。假设您在if和&&之间有2条陈述。如果在第一个语句中使用可选绑定创建一个let,则