jayoguntino
jayoguntino

Reputation: 133

I am trying to create a method that calculates the averages of numbers in two arrays

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() {


}

}

I am trying to access the numbers in finalArray and finalArray2 but I am not sure how to do this when I try to call on the two arrays it does not work I think it is to do with the scope of the arrays.So how do I make the two array accessible to the whole program.

Upvotes: 0

Views: 80

Answers (2)

blalasaadri
blalasaadri

Reputation: 6198

While you could make the arrays visible to the whole class, it probably makes more sense to pass them to the method which should access them. So do something like this:

public void calculateAvg(double[] array1, double[] array2) {
    // ...
}

You can then call it like this:

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

Upvotes: 0

Kyle
Kyle

Reputation: 29

If you want to make the two arrays accessible to the whole program, declare them as static variables outside of the main method and initialize inside like seen here:

    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();

Upvotes: 1

Related Questions