import java.io.*;
import java.util.*;
public class Marks {
public static void main(String[] args) {
Marks r = new Marks();
double[] finalArray = r.openFile();
double[] finalArray2 = r.openFile2();
}
//ID's and first set of grades
private Scanner a;
public double[] openFile() {
ArrayList<String> list1 = new ArrayList<String>(7);
try {
a = new Scanner(new File("IR101.txt"));
} catch (Exception e) {
System.out.println("could not find file");
}
while (a.hasNextLine()) {
list1.add(a.nextLine());
}
String[] arrayOne = list1.toArray(new String[list1.size()]);
Arrays.sort(arrayOne);
//System.out.println(Arrays.toString(arrayOne));
int size = arrayOne.length;
double[] finalArray = new double[size];
for (int j = 0; j < size; j++) {
String word = arrayOne[j];
String newWord = word.substring(6, 10);
double grade = Double.parseDouble(newWord);
finalArray[j] = grade;
}return finalArray;
}
//ID's and second set of grades
private Scanner b;
public double[] openFile2() {
ArrayList<String> list2 = new ArrayList<String>(7);
try {
b = new Scanner(new File("IR102.txt"));
} catch (Exception e) {
System.out.println("could not find file");
}
while (b.hasNextLine()) {
list2.add(b.nextLine());
}
String[] arrayTwo = list2.toArray(new String[list2.size()]);
Arrays.sort(arrayTwo);
//System.out.println(Arrays.toString(arrayTwo));
int size = arrayTwo.length;
double[] finalArray2 = new double[size];
for (int j = 0; j < size; j++) {
String word = arrayTwo[j];
String newWord = word.substring(6, 10);
double grade2 = Double.parseDouble(newWord);
finalArray2[j] = grade2;
}return finalArray2;
}
// ID's and names
private Scanner c;
public void openFile3() {
ArrayList<String> list3 = new ArrayList<String>(7);
try {
c = new Scanner(new File("IRStudents.txt"));
} catch (Exception e) {
System.out.println("could not find file");
}
while (c.hasNextLine()) {
list3.add(c.nextLine());
}
String[] arrayThree = list3.toArray(new String[list3.size()]);
Arrays.sort(arrayThree);
//System.out.println(Arrays.toString(arrayThree));
int size = arrayThree.length;
String[] names = new String[size];
for (int j = 0; j < size; j++) {
names[j] = arrayThree[j].substring(6);
}
String[] IDs = new String[size];
for (int x = 0; x < size; x++){
IDs[x] = arrayThree[x].substring(0,5);
}
System.out.println(Arrays.toString(names));
System.out.println(Arrays.toString(IDs));
}
public void calculateAvg() {
}
}
我正在尝试访问finalArray和finalArray2中的数字,但我不知道如何在我尝试调用两个数组时这样做它不起作用我认为它与数组的范围有关。所以如何使两个数组可以访问整个程序。
答案 0 :(得分:1)
如果要使整个程序可以访问这两个数组,请将它们声明为main方法之外的静态变量,并在内部初始化,如下所示:
public class Marks {
static double[] finalArray;
static double[] finalArray2;
public static void main(String[] args) {
Marks r = new Marks();
finalArray = r.openFile();
finalArray2 = r.openFile2();
答案 1 :(得分:0)
虽然您可以使数组对整个类可见,但将它们传递给应该访问它们的方法可能更有意义。所以做这样的事情:
public void calculateAvg(double[] array1, double[] array2) {
// ...
}
然后你可以这样称呼它:
public static void main(String[] args) {
Marks r = new Marks();
double[] finalArray = r.openFile();
double[] finalArray2 = r.openFile2();
r.calculateAvg(finalArray, finalArray2);
}