制作读取和写入数据的程序 - 然后计算最小值,最大值,平均值

时间:2015-02-10 17:05:04

标签: java arrays methods

我正在创建一个程序,从文本文件中读取一些数据,然后获取该数据并查找数字的最小值,最大值和平均值。出于某种原因,我遇到了许多我以前从未见过的荒谬错误。这是我的代码:

import java.io.File;
import java.util.Scanner;
import java.io.FileWriter;

public class Lab1 {
static int count = 0;
static int[] newData2 = new int[count];


// Method for reading the data and putting it into different arrays
static int[] readData() {
    File f = new File("data.txt");
    int[] newData = new int[100];
    try {
        Scanner s = new Scanner(f);
        while (s.hasNext()) {
            newData[count++] = s.nextInt();

        }

        for (int i = 0; i < newData2.length; i++) {
            newData[i] = newData2[i];
            return newData2;
        }

    } catch (Exception e) {
        System.out.println("Could not read the file.");
    }
}

static int min(int[] newData2) {
    int min = newData2[0];
    for (int i = 0; i < newData2.length; i++) {
        if (newData2[i] < min) {
            min = newData2[i];
        }

    }
    return min;

}

static int max(int[] newData2) {
    int max = newData2[0];
    for (int i = 0; i < newData2.length; i++) {
        if (newData2[i] > max) {
            max = newData2[i];
        }
    }
    return max;
}

static double average(int[] newData2) {
    double average = 0;
    int sum = 0;

    for (int i = 0; i < newData2.length; i++) {
        sum = newData2[i];
    }
    average = sum / newData2.length;
    return average;
}

/*
 * static int stddev(int[] newData2) { int[] avgDif = new
 * int[newData2.length]; for(int i = 0; i < newData2.length; i++) {
 * avgDif[i] = (int) (average(newData2) - newData2[i]); }
 * 
 * }
 */

void write(String newdata, int min, int max, double average, int stddev) {
    try {
        File file = new File("stats.txt");      
        file.createNewFile();
        FileWriter writer = new FileWriter("stats.txt");
        writer.write("Minimum: " + min + "Maximum: " + max + "Average: " + average);
        writer.close();
}catch(Exception e) {
    System.out.println("Unable to write to the file.");
}

public static void main(String[] args) {


}

}
}

我的readData方法出错了,它告诉我:

  

此方法必须返回int []的结果类型。

我实际上是返回一个int数组,所以我不明白这里的问题是什么。

然后在我的main方法中,它表示void是变量main的无效类型。

2 个答案:

答案 0 :(得分:0)

static int[] readData() {
    File f = new File("data.txt");
    int[] newData = new int[100];
    try {
        Scanner s = new Scanner(f);
        while (s.hasNext()) {
            newData[count++] = s.nextInt();

        }

        for (int i = 0; i < newData2.length; i++) {
            newData[i] = newData2[i];
            return newData2;
        }

    } catch (Exception e) {
        System.out.println("Could not read the file.");
    }

    //TODO: return something here if there is some kind of error
}

由于try-catch块,您需要考虑可能发生的每种可能性。当程序成功时返回数组时,您期望返回,但是当存在异常时程序仍然需要返回值,但是您没有提供返回值。

答案 1 :(得分:0)

以下是一些提示:

  1. 返回某个东西的方法的每个出口点必须返回一些东西,如果行new Scanner(f);抛出异常,则不会达到第一个返回,所以你需要一个默认值,如下所示:
  2. private int[] readData() {
        File f = new File("data.txt");
        int count = 0;
        int[] newData = new int[100];
        try {
            Scanner s = new Scanner(f);
            while (s.hasNext()) {
                newData[count++] = s.nextInt(); // maybe you should handle the case where your input is too large for the array "newData"
            }
            return Arrays.copyOf(newData, count);
        } catch (Exception e) {
            System.out.println("Could not read the file.");
        }
    
        return null;
    }
    
    1. 要减小数组的大小,您应该使用Arrays.copyOf(见下文)
    2. 你应该避免使用静态字段(在你的情况下不需要静态字段)
    3. 您的方法minmax假设数组中有元素(至少有一个),您不应该这样做(或使用if测试):
    4. private int min(int[] data) {
          int min = Integer.MAX_VALUE; // handy constant :)
          for (int i = 0; i < data.length; i++) {
              if (data[i] < min) {
                  min = data[i];
              }
          }
          return min;
      }
      
      private int max(int[] data) {
          int max = 0;
          for (int i = 0; i < data.length; i++) {
              if (data[i] > max) {
                  max = data[i];
              }
          }
          return max;
      }
      
      1. average方法中,有一些错误:
      2. private double average(int[] data) {
            int sum = 0;
            for (int i = 0; i < data.length; i++) {
                sum += data[i]; // here if you want a sum it's += not =
            }
            return (1.0 * sum) / data.length; // you want a double division, local "average" was useless
        }
        
        1. 数组是可迭代的,所以你可以使用&#34; new style&#34; for loops:
        2. for (int value : newData) {
              // use value
          }
          

          有些人阅读: