Calculate the averages of two different tables

advertisements

I am trying to calculate the averages of two grades in two different arrays but I can not access them.

How do I access both the finalArray1 and finalArray values such that I will be able to calculate the averages of both numbers and add them to the array called averages.

I think it is due to the scope of the two arrays. How do I change the program so that I will be able to access the Arrays from anywhere in the program?

private Scanner a;

public void 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;
    }
    System.out.println(Arrays.toString(finalArray));
}
//ID's and second set of grades

private Scanner b;

public void 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;
    }
    System.out.println(Arrays.toString(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);
    }
    System.out.println(Arrays.toString(names));
}


I would suggest you simply create ArrayList return types for your "openFile" methods so that it will return the lists for you to manipulate.

I am not sure of your implementation of the code, but this is what it will look like:


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;
    //System.out.println(Arrays.toString(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;
    //System.out.println(Arrays.toString(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);
    }
    System.out.println(Arrays.toString(names));

    }

}

And your implementation of the code would be as such:


UPDATED IMPLEMENTATION

public static void main(String[] args){
    Run r = new Run();
    double[] finalArray = r.openFile();
    double[] finalArray2 = r.openFile2();
}

From this point forward you can now manipulate the contents of both arrays and work out your average for them.

I hope I was of some assistance.

Let me know of the outcome!