I'm currently taking my first java class and have become completely stuck on an exercise. I'm supposed to read data from a text file containing student IDs and their corresponding test scores, have the program grade them then print the results.
我目前正在参加我的第一个java课程并且完全陷入了练习。我应该从包含学生ID及其相应测试分数的文本文件中读取数据,让程序对它们进行分级,然后打印结果。
I kind of understand the problem, but the book we're working from is kinda hard to read. It all blurs together and I feel like they want me to read two separate things and make a logical leap on how to put them together and I just don't get it.
我有点理解这个问题,但我们正在编写的这本书有点难以阅读。这一切都在一起模糊,我觉得他们希望我阅读两个不同的东西,并在如何将它们组合在一起的逻辑上跳跃,我只是不明白。
TTFTFTTTFTFTFFTTFTTF ABC54102 T FTFTFTTTFTTFTTF TF DEF56278 TTFTFTTTFTFTFFTTFTTF ABC42366 TTFTFTTTFTFTFFTTF ABC42586 TTTTFTTT TFTFFFTF
TTFTFTTTFTFTFFTTFTTF ABC54102 T FTFTFTTTFTTFTTF TF DEF56278 TTFTFTTTFTFTFFTTFTTF ABC42366 TTFTFTTTFTFTFFTTF ABC42586 TTTTFTTT TFTFFFTF
My main issue is that I don't see how I tie the array to the data I have.
我的主要问题是我没有看到如何将数组绑定到我拥有的数据。
2 个解决方案
#1
3
I am not gonna post the whole solution but give some steps to start.
我不打算发布整个解决方案,但要给出一些步骤。
Follow this example
请遵循此示例
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);
}
and to split the string like this
并像这样拆分字符串
str.split(" "); // considering that ids and name are separated by spaces
#2
0
So, since blanks are allowed in your list of T's and F's, which I assume means the student left the answer to the question blank, you do not have the luxury of using a convenience method like split
to easily separate the answers. Instead we use our knowledge that the number of questions must be the same, and id's must have a common length. You can use the substring method to parse out the what you need.
因此,由于在你的T和F列表中允许使用空格,我认为这意味着学生将问题的答案留空了,你没有使用像拆分这样的便利方法来轻松分离答案。相反,我们使用我们的知识,问题的数量必须相同,并且id必须具有共同的长度。您可以使用substring方法来解析您需要的内容。
Here's some pseudocode:
这是一些伪代码:
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);
}
#1
3
I am not gonna post the whole solution but give some steps to start.
我不打算发布整个解决方案,但要给出一些步骤。
Follow this example
请遵循此示例
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);
}
and to split the string like this
并像这样拆分字符串
str.split(" "); // considering that ids and name are separated by spaces
#2
0
So, since blanks are allowed in your list of T's and F's, which I assume means the student left the answer to the question blank, you do not have the luxury of using a convenience method like split
to easily separate the answers. Instead we use our knowledge that the number of questions must be the same, and id's must have a common length. You can use the substring method to parse out the what you need.
因此,由于在你的T和F列表中允许使用空格,我认为这意味着学生将问题的答案留空了,你没有使用像拆分这样的便利方法来轻松分离答案。相反,我们使用我们的知识,问题的数量必须相同,并且id必须具有共同的长度。您可以使用substring方法来解析您需要的内容。
Here's some pseudocode:
这是一些伪代码:
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);
}