3

i have a txt of marks for students, every line being a new student. i want to add all the first marks of every student to an array, then all the second marks to a different array etc. every array is the module, i then have methods to find the mean and median etc of these module marks, but im struggling reading every line then adding the first values of every 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 the 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

}

sb33
  • 67
  • 7
  • 2
    Can you please narrow down your question to something more specific? As it stands right now, I think I would have to pull your code into IntelliJ and debug it to see what is going wrong. – Tim Biegeleisen Mar 21 '17 at 15:32
  • @TimBiegeleisen i want the first item of each line to be added to the first array essentially – sb33 Mar 21 '17 at 15:33
  • what is the format of marks.txt? – brad Mar 21 '17 at 15:36
  • @brad its 130 rows of 17 numbers eg: -1, 20, 35, 45, -1 10...etc -1 meaning they did not take the module – sb33 Mar 21 '17 at 15:38
  • First of all I think you should use List instead of Array, because arary doesn't have the add method and are statics it means that you cannot change the size of the array, but you can change the size of the list. Also we need an example of the content of marks.txt and what you are desire to store in those java objects. – Brank Victoria Mar 21 '17 at 15:39
  • @BrankVictoriai know there isnt an add method, its just that the mean median etc methods all use arrays not lists so i wasnt too sure if it was smart to change how they work – sb33 Mar 21 '17 at 15:42
  • Please add an excerpt of your text file. Of course, obscure the student's names first. – domsson Mar 21 '17 at 15:53
  • @domdom there are no names or any other info - its just the marks: 56,-1,-1,48,-1,-1,41,18,66,24,13,60,-1,-1,-1,-1,41 (new line) 43,-1,-1,-1,-1,41,44,-1,-1,-1,34,50,47,39,32,-1,41 – sb33 Mar 21 '17 at 15:55

2 Answers2

1

I would use lists instead of arrays, because arrays doesn't have an "Add2 method, you should also use BufferedReader object so you can read your text file line by line, considering each line is a mark you can use a counter for know in which line are you for know which list method you have to store it. 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 file marks.txt, in this case if every line is separated by "," you should do an split and then in the array object resulted you every index correspond with 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;
                }
            }

        }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Brank Victoria
  • 1,447
  • 10
  • 17
0

First off, read your file into an array of lines. 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 a coma-separated line into its constituents using this SO question:

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

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

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

This makes it easy to not repeat your 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]);
            }
        }
    }
}

You can then get any course grades using:

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

Edit: Seeing as your data does not have student name, I agree with using Arrays everywhere, because their staticity will protect the ordering, which is the only guarantee that your data makes sense. otherwise I would rather have (as usual) a List of Student Object with each their own Map<Course, Double> myGrades field. Much more OOP.

Community
  • 1
  • 1
MrBrushy
  • 680
  • 3
  • 17