计算两个不同数组的平均值

时间:2015-04-15 18:48:05

标签: java arrays

我正在尝试计算两个不同阵列中两个等级的平均值,但我无法访问它们。

如何访问finalArray1finalArray值,以便我能够计算两个数字的平均值并将它们添加到名为averages的数组中。

我认为这是由于两个数组的范围。如何更改程序,以便我可以从程序中的任何位置访问数组?

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

2 个答案:

答案 0 :(得分:1)

我建议您只为“openFile”方法创建ArrayList返回类型,以便它返回列表供您操作。

我不确定您的代码实现,但这就是它的样子:


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

    }

}


您的代码实现方式如下:


  

更新后的实施

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

从现在开始,您现在可以操纵两个数组的内容并计算出它们的平均值。

我希望我有所帮助。

让我知道结果!

答案 1 :(得分:0)

假设这个片段在一个类中(让我们称之为Klass),你可以将数组声明为属性而不是局部变量。 E.g:

public class Klass {

   private double[] finalArray;
   private double[] finalArray2;

   //...
   //your methods, skipping the finalArray AND finalArray2 declaration
   //...
   public void calculateAvg(){
       //your average alg
       System.out.println("finalArray.length" + this.finalArray.length);
   }
}

我也会使用java Collections(例如java.util.List)。

希望有所帮助