将int从文件添加到数组

时间:2017-03-21 15:30:04

标签: java arrays java.util.scanner

我为学生提供了一个标记,每一行都是新生。我想将每个学生的所有第一个标记添加到一个数组中,然后将所有第二个标记添加到不同的数组等。每个数组都是模块,然后我有方法找到这些模块标记的均值和中位数等,但我努力阅读每一行然后添加每行的第一个值等。

public interface StatCalculator {
   double[] CE1014FY = {};
   double[] CE1014SP = {};
   double[] CE1414AU = {};
   double[] CE1414FY = {};
   double[] CE1424AU = {};
   double[] CE1424FY = {};
   double[] CE1514AU = {};
   double[] CE1524SP = {};
   double[] CE1534AU = {};
   double[] CE1544SP = {};
   double[] CE1554SP = {};
   double[] CE1614AU = {};
   double[] CE1624SP = {};
   double[] CE1634AU = {};
   double[] CE1644SP = {};

   static void get(){
     try {
        File file = new         File("M:\\Documents\\ce201\\subversion\\resources\\marks.txt");
        Scanner scanner = new Scanner(file);
       for every line
          CE1014FY.add(line[0];         //i want something like this
          CE1014SP.add(line[1]

        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }


}

static double mean(double[] numbers){

    double sum = 0;
    for (int i = 0; i < numbers.length; i++) {
        sum += numbers[i];

    }
    System.out.println("mean: "+sum/numbers.length);
    return sum / numbers.length;

}

public static double median(double[] numbers) {
    Arrays.sort(numbers);
    int middle = numbers.length/2;
    if (numbers.length%2 == 1) {
        System.out.println("median: "+numbers[middle]);
        return numbers[middle];
    } else {
        System.out.println("median: "+(numbers[middle-1] + numbers[middle]) / 2.0);
        return (numbers[middle-1] + numbers[middle]) / 2.0;

    }
}

public static double lowerQ(double[] numbers) {
    Arrays.sort(numbers);
    int lqpos = (numbers.length+1)/4;
    int lq= (int) (int) numbers[lqpos];
    System.out.println("lower q: "+lq);
    return lq;
}

public static double upperQ(double[] numbers) {
    Arrays.sort(numbers);
    int uqpos = 3*(numbers.length+1)/4;
    int uq= (int) (int) numbers[uqpos];
    System.out.println("upper q: "+uq);
    return uq;
}

public static void main(String[] args) {
    get();
    mean(CE1014FY);
    median(CE1014FY);
    upperQ(CE1014FY);
    lowerQ(CE1014FY);
}

}

然后是txt {

  63,-1,-1,76,-1,-1,82,85,84,57,67,73,-1,-1,-1,-1,73
  62,-1,-1,60,-1,-1,89,76,79,53,55,77,-1,-1,-1,-1,69
  60,-1,-1,42,-1,-1,37,32,67,-1,44,56,37,-1,-1,-1,47
  53,-1,-1,88,-1,-1,75,68,69,58,64,75,-1,-1,-1,-1,69
  72,-1,-1,64,-1,-1,39,55,74,56,78,64,-1,-1,-1,-1,63
  50,-1,-1,30,-1,-1,19,20,35,19,7,34,-1,-1,-1,-1,27

}

2 个答案:

答案 0 :(得分:1)

我会使用列表而不是数组,因为数组没有&#34; Add2方法,您还应该使用BufferedReader对象,这样您就可以逐行读取文本文件, line是一个标记,你可以使用一个计数器知道你在哪一行知道你必须存储它的列表方法。所以你的get方法应该是这样的:

   static void get(){
     try {
        File file = new File("M:\\Documents\\ce201\\subversion\\resources\\marks.txt");
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            int count = 1;
            while((line = br.readLine()) != null)
            {
                switch(count)
                {
                    case 1:
                        CE1014FY.add(Double.parseDouble(line));
                        break;
                    case 2:
                        CE1014SP.add(Double.parseDouble(line));
                        break;
                    case 3:
                        CE1414AU.add(Double.parseDouble(line));
                        break;
                }
            count++;
            }

}

修改

我只看到你的文件marks.txt的格式,在这种情况下,如果每一行用&#34;,&#34;你应该进行拆分然后在数组对象中导致每个索引都对应一个列表,在这种情况下,你的get方法

   static void get(){
     try {
        File file = new File("M:\\Documents\\ce201\\subversion\\resources\\marks.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while((line = br.readLine()) != null)
        {
            String[] lineSplitted = line.split(",");
            for(int i=0; i<lineSplitted.length; i++)
            {
                switch(i)
                {
                    case 0:
                        CE1014FY.add(Double.parseDouble(lineSplitted[i]));
                        break;
                    case 1:
                        CE1014SP.add(Double.parseDouble(lineSplitted[i]));
                        break;
                    case 2:
                        CE1414AU.add(Double.parseDouble(lineSplitted[i]));
                        break;
                }
            }

        }

}

答案 1 :(得分:0)

首先,将您的文件读入一系列行。使用this SO question中的此方法:

public String[] readLines(String filename) throws IOException {
    FileReader fileReader = new FileReader(filename);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    List<String> lines = new ArrayList<String>();
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        lines.add(line);
    }
    bufferedReader.close();
    return lines.toArray(new String[lines.size()]);
}

其次,使用this SO question

将昏迷分隔线分成其成分
String[] grades = line.split(",")

为避免在整个地方重复使用表格,请使用enum

public enum Course {
    CE1014FY, CE1014SP, CE1414AU, CE1414FY, CE1424AU, CE1424FY, CE1514AU,
    CE1524SP, CE1534AU, CE1544SP, CE1554SP, CE1614AU, CE1624SP, CE1634AU, CE1644SP;
}

这样可以轻松不重复您的代码。

我们现在拥有所需的一切:

static Map<Courses, double[]> grades = new HashMap<>();
static void get(){
    String[] lines = readLines("M:\\Documents\\ce201\\subversion\\resources\\marks.txt");
    // Init the grades map
    for(Course course: Course.values()) {
        grades.put(course, new double[lines.length]);
    }
    // Fill the grades map
    int lineNum = 0;
    for(String line: lines){
        String[] grades = line.split(",");
        int courseNum = 0;
        for(Course course: Course.values()) {
            try{
                grades.get(course)[lineNum] = Double.parseDouble(grades[courseNum++]);
            } catch(NumberFormatException nfe){
                grades.get(course)[lineNum] = -1;
                System.out.println("Error reading grade for course " + course + ", line " + lineNum + " : " + grades[courseNum-1]);
            }
        }
    }
}

然后您可以使用以下方式获得任何课程成绩:

double[] gradesCE1414AU = grades.get(course.CE1414AU);

编辑:看到您的数据没有学生姓名,我同意在任何地方使用Arrays,因为它们的静态性将保护排序,这是您的数据有意义的唯一保证。否则我宁愿(像往常一样)拥有ListStudent对象,每个对象都有自己的Map<Course, Double> myGrades字段。更多的OOP。