【发布时间】:2019-11-03 22:15:28
【问题描述】:
我目前正在尝试开发一个程序,该程序使用学生 ID 和 GPA(取自 txt 文件)并使用它们来做许多其他事情,例如根据 GPA 范围将学生分为 8 个类别中的 1 个,制作直方图每个组的学生,并按 GPA 对学生进行排名。但是,我需要做的第一件事是将学生 ID 和 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
}
}
所以我的问题是:如何将这些信息传递到两个单独的数组中?如果我的问题难以理解,我深表歉意,我对编程非常陌生。我已经被这个程序难倒了很长时间,任何帮助将不胜感激!谢谢。
【问题讨论】:
标签: java arrays file java.util.scanner