我希望接受文件输入并以一种方法连续使用它。有没有一种方法可以使我的扫描仪保持静态,以便每次使用该方法时都可以从数字列表中的一行移到另一行?
在将扫描仪移至主要方法内部时,我做了很多修补工作,但这使我失败了。同样,我也尝试过将扫描仪放入正在使用它的方法中,但是每次使用都会创建一个新的扫描仪,重新读取数据的第一行。
public class courseExaminerMJE {
static File file = new
File("C:\\Users\\goati_000\\Downloads\\grades.txt"); //using a local path is not working for me
public static Scanner sc = new Scanner(file);
public static int[] stu1cis, stu1eng, stu1mth, stu1chm, stu2cis, stu2eng, stu2mth, stu2chm;
//within each of the arrays is all of the grades and then the average in the [10] position
public static void main(String[] args) throws FileNotFoundException {
stu1cis = new int[11];
stu1eng = new int[11];
stu1mth = new int[11];
stu1chm = new int[11];
stu2cis = new int[11];
stu2eng = new int[11];
stu2mth = new int[11];
stu2chm = new int[11];
averageData(stu1cis); //should have 62 in stu1cis[9]
averageData(stu1eng); //should have 75 in stu1eng[9]
averageData(stu1mth);
averageData(stu1chm);
averageData(stu2cis);
averageData(stu2eng);
averageData(stu2mth);
averageData(stu2chm);
}
public static void averageData(int[] a) throws FileNotFoundException {
int total = 0;
for (int i = 0; i < 10; i++) {
a[i] = sc.nextInt();
total += a[i];
}
a[10] = total / 10;
sc.nextLine(); //allows me to cycle to the next row of numbers, avoiding -999
}
}
我从中获取的数据如下:
80 60 85 71 92 86 85 88 82 62 -999
62 55 72 82 78 69 82 78 81 75 -999
88 85 88 92 95 95 88 88 90 92 90 -999
61 68 62 75 77 65 78 79 82 75 -999
32 45 88 67 58 72 76 67 60 85 -999
88 89 82 90 88 84 82 80 78 89 -999
85 81 78 76 82 86 81 80 78 79 -999
82 96 98 88 92 95 96 88 89 90 -999
它在文本文档中的第3行的文件路径中。