我在此代码中遇到问题。我想要做的是读取一个文件并存储一个学生ID并将一个分数数组分成一个学生对象的分数属性,但我只有在打印时才能获得最后的分数。这是代码。你能告诉我,我的setter属性是否是在学生班级中分配数组的正确方法?问题是得分文件的最后一行存储在每个数组中,即使我调试它时我看到得分数组被传递,而且studentID数组工作正常。
import lab6.*;//importing the necessary classes
public class Main
{
public static void main(String[] args)
{
Student lab6 [] = new Student[40];
//Populate the student array
lab6 = Util.readFile("studentScores.txt", lab6);
lab6[4].printStudent();
}
}
学生班------------------------------------
package lab6;
public class Student
{
private int SID;
private int scores[] = new int[5];
//write public get and set methods for SID and scores
public int getSID()
{
return SID;
}
public void setSID(int SID)
{
this.SID = SID;
}
public int[] getScores()
{
return scores;
}
public void setScores(int scores[])
{
this.scores = scores;
}
//add methods to print values of instance variables.
public void printStudent()
{
System.out.print(SID);
System.out.printf("\t");
for(int i = 0; i < scores.length; i++)
{
System.out.printf("%d\t", scores[i]);
}
}
}
the util class --------------------------------------------------------------------
import java.io.*;
import java.util.StringTokenizer;
//Reads the file and builds student array.
//Open the file using FileReader Object.
//In a loop read a line using readLine method.
//Tokenize each line using StringTokenizer Object
//Each token is converted from String to Integer using parseInt method
//Value is then saved in the right property of Student Object.
public class Util
{
public static Student [] readFile(String filename, Student [] stu)
{
try {
String line[] = new String[40];//one line of the file to be stored in here
StringTokenizer stringToken;
int studentID;//for storing the student id
int[] studentScoreArray = new int[5];//for storing the student score
FileReader file = new FileReader(filename);
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
int i = 0;
buff.readLine();//used this to skip the first line
while (!eof) //operation of one line
{
line[i] = buff.readLine();
if (line[i] == null)
eof = true;
else //tokenize and store
{
stringToken = new StringTokenizer(line[i]);
String tokenID = stringToken.nextToken().toString();//for storing the student id
studentID = Integer.parseInt(tokenID);
stu[i] = new Student();//creating student objects
stu[i].setSID(studentID);//stored in student object
//now storing the score-------------------------------------------------
int quizNumberCounter = 0;
while (stringToken.hasMoreTokens())
{
String tokens = stringToken.nextToken().toString();
studentScoreArray[quizNumberCounter] = Integer.parseInt(tokens);//converting and storing the scores in an array
quizNumberCounter++;//array progression
}
stu[i].setScores(studentScoreArray);//setting the score(passing it as an array)
//-----------------------------------------------------------------------
}
i++;
}
buff.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
return stu;
}
/*
StringTokenizer st = new StringTokenizer("this is a test");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
//How to convert a String to an Integer
int x = Integer.parseInt(String) ;*/
}
示例文件结构-------------------------------------------- -----------
4532 011 017 081 032 077
答案 0 :(得分:2)
问题在于
int[] studentScoreArray = new int[5];
你必须在学生循环中移动这个并为每个学生初始化数组。否则你会为所有学生重复使用相同的数组(即内存),并且你会一遍又一遍地覆盖分数。
// int[] studentScoreArray = new int[5]; // <= line removed here!!!
...
while (!eof) //operation of one line
{
line[i] = buff.readLine();
if (line[i] == null)
eof = true;
else //tokenize and store
{
int[] studentScoreArray = new int[5]; // <= line moved over to here!!!
...
}
}
答案 1 :(得分:0)
您可能只看到一行结果的一个原因是您只打印一行结果:
lab6[4].printStudent();
如果要查看所有结果,则需要将其更改为循环遍历数组:
foreach (Student student : lab6)
{
student.printStudent();
}
另外,您的数组可能会被称为students
而不是lab6
。在java中使用Type[] identifier
而不是Type identifier []
来声明数组也是惯用的。
免责声明:可能还有其他错误,我没有阅读所有发布的数百行!
答案 2 :(得分:0)
我没有根据我的建议测试代码,但请看一下:
int[] studentScoreArray = new int[5];
您只为整个文件创建一次和一次。 一个简单而容易的解决方法就是为每个新行读取它。
像这样:int[] studentScoreArray = new int[5];
int quizNumberCounter = 0;
while(..) { ...}