我目前正在尝试使用学生ID和GPA(从txt文件中获取)并使用它们来做许多其他事情的程序,例如根据GPA范围将学生分为8个类别中的1个,进行直方图每个组中的学生数,并按GPA对学生进行排名。但是,我要做的第一件事是将学生证和GPA转移到两个单独的数组中。
我知道创建数组的语法如下:
elementType[] arrayRefVar = new elementType[arraySize]
但是,我仍然不知道如何将从文件中读取的数据传递到两个单独的数组中。我必须从txt文件读取数据的代码如下:
public static void main(String[] args) throws Exception // files requires exception handling
{
String snum;
double gpa;
Scanner gpadata = new Scanner(new File("studentdata.txt"));
while (gpadata.hasnext()) // loop until you reach the end of the file
{
snum = gpadata.next(); // reads the student's id number
gpa = gpadata.nextDouble(); // read the student's gpa
System.out.println(snum + "\t" + gpa); // display the line from the file in the Output window
}
}
所以我的问题是:如何将这些信息传递到两个单独的数组中?如果我的问题难以理解,我深表歉意。我对编程非常陌生。我已经为此程序困扰了很长时间了,任何帮助将不胜感激!谢谢。