Java 将文本文件中的数据处理到数组中

Java 将文本文件中的数据处理到数组中,java,arrays,text,Java,Arrays,Text,我目前正在学习我的第一个java课程,并且已经完全陷入了一个练习中。我应该从一个包含学生ID和他们相应的考试成绩的文本文件中读取数据,让程序给他们打分,然后打印结果 我有点理解这个问题,但是我们正在研究的那本书有点难读。这一切都模糊在一起,我觉得他们想让我读两个不同的东西,然后在如何把它们放在一起的问题上做出逻辑上的飞跃,但我就是不明白 TTFTFTTTFTFFTTFTTF ABC54102 T FTFTFTTTFTTF TF DEF56278 TTFTFTTTFTFFTTFTTF ABC423

我目前正在学习我的第一个java课程,并且已经完全陷入了一个练习中。我应该从一个包含学生ID和他们相应的考试成绩的文本文件中读取数据,让程序给他们打分,然后打印结果

我有点理解这个问题,但是我们正在研究的那本书有点难读。这一切都模糊在一起,我觉得他们想让我读两个不同的东西,然后在如何把它们放在一起的问题上做出逻辑上的飞跃,但我就是不明白

TTFTFTTTFTFFTTFTTF ABC54102 T FTFTFTTTFTTF TF DEF56278 TTFTFTTTFTFFTTFTTF ABC42366 TTFTFTTTFTFFTTF ABC42586 TTTTFTTTFFFTF


我的主要问题是,我不知道如何将数组与我拥有的数据联系起来。

我不会发布整个解决方案,但会给出一些开始步骤

遵循这个例子

BufferedReader reader = new BufferedReader(new FileReader("/path/to/file.txt"));
String line = null;
ArrayList<String> array = new ArrayList<>();
while ((line = reader.readLine()) != null) {
    array.add(line);
}

因此,由于在你的T和F列表中允许空白,我认为这意味着学生将问题的答案留空,因此你没有机会使用方便的方法,如
split
轻松地将答案分开。相反,我们使用我们的知识,即问题的数量必须相同,id的长度必须相同。您可以使用substring方法解析出所需的内容

下面是一些伪代码:

final int NUM_QUESTIONS = 25; //I didn't actually count, that's your job
final int ID_LENGTH = 8;
int currentIndex = 0;

//assuming you can fit the whole string in memory, which you should in an intro java class
//do the operations that googling "read a file into a string java" tells you to do in readFileToString
String fileContents = readFileToString("saidFile.txt");


while(fileContents.charAt(currentIndex) != fileContents.length()){
    String userAnswers = fileContents.substring(currentIndex, currentIndex+NUM_QUESTIONS);

    //move index past userAnswers and the space that separates the answers and the id
    currentIndex = currentIndex + NUM_QUESTIONS + 1;

    String userId = fileContents.substring(currentIndex, currentIndex+ID_LENGTH)

    //move currentIndex past userId and the space that separates the userId from the next set of answers
    currentIndex = currentIndex + ID_LENGTH + 1;

    //either create an object to store the score with the userId, or print it right away
    int score = gradeAnswers(userAnswers)
    System.out.println(userId + " scored " + score);
}

文本文件必须有某种分隔符,就像它可能是csv文件或其他文件一样。没有你的帖子我就说不出来。但是读入文件后,每一行都按任何分隔符进行分割,通过其列索引获得分数、id等。学习编程的开始是了解谷歌的正确做法。很明显,您不会这样做,我建议您进行以下搜索:“如何逐行读取文件java”,“如何在逗号java上拆分字符串”(或者,在给定的文件中,内容是如何分隔的)。您可能应该对从文件中读取的内容进行对象表示,并将其存储在数组列表中。诚实地编辑你的帖子,以显示文件的外观,我可以帮助你进一步查看,但这正是我所困惑的。我在这个主题上看到的每一件事都是围绕着分裂绳子或做x或y的东西,而这本书却没有提到任何一件事。我的答案有帮助吗?如果是,则将其标记为正确。谢谢,缓冲阅读器到底是什么?我可以有一些详细说明每个步骤具体做什么的评论吗?简单地说,BufferedReader用于I/O,即读取和写入文件。细节又来了,我不知道从哪里开始。这是个该死的兔子洞。我最终遇到的问题比我开始时多,当这本书略过四到五个不同术语的定义,然后像我知道它们的意思一样使用它们时,这本书没有任何帮助。
final int NUM_QUESTIONS = 25; //I didn't actually count, that's your job
final int ID_LENGTH = 8;
int currentIndex = 0;

//assuming you can fit the whole string in memory, which you should in an intro java class
//do the operations that googling "read a file into a string java" tells you to do in readFileToString
String fileContents = readFileToString("saidFile.txt");


while(fileContents.charAt(currentIndex) != fileContents.length()){
    String userAnswers = fileContents.substring(currentIndex, currentIndex+NUM_QUESTIONS);

    //move index past userAnswers and the space that separates the answers and the id
    currentIndex = currentIndex + NUM_QUESTIONS + 1;

    String userId = fileContents.substring(currentIndex, currentIndex+ID_LENGTH)

    //move currentIndex past userId and the space that separates the userId from the next set of answers
    currentIndex = currentIndex + ID_LENGTH + 1;

    //either create an object to store the score with the userId, or print it right away
    int score = gradeAnswers(userAnswers)
    System.out.println(userId + " scored " + score);
}