Adding int from file to array

I have txt of labels for students, each line is a new student. I want to add all first labels of each student to an array, then all other labels to another array, etc. each array is a module, then I have methods to find mean and mean, etc. of these module labels, but im struggling to read each line, then adding the first values ​​of each line, etc.

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

      

}

then 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

      

}

+3


source to share


2 answers


I would use lists instead of arrays, because arrays do not have an "Add2" method, you must also use an object BufferedReader

so you can read the text file line by line, given that each line is a marker that you can use to search the counter. for which line you know which list method you should store, so your get method should look like this:

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

}

      

edit



I just see the format of your marks.txt file, in this case if each line is separated by "," you have to do the splitting and then in the array you provided, each index matches a list in this case, your get method

   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


source


First of all, read your file as an array of strings. Use this method from 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()]);
}

      

Second, split the comically highlighted line into its constituents using this SO question :

String[] grades = line.split(",")

      

To avoid repeating your tables all over the place, use enum

:

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

      



This makes it easier to repeat code.

We now have everything we need:

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

      

Then you can get any grades for the course using:

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

      

Edit: Seeing that your data doesn't have a student name, I agree with using arrays everywhere because being static will protect the order, which is the only guarantee that your data makes sense. otherwise I would prefer (as usual) an List

object Student

with each of their own fields Map<Course, Double> myGrades

. Much more OOP.

0


source







All Articles