-2

I have this String array in which is made up of string representing a student id, first name, last name, email,and grades. My question is: Is there away I can split each entry in this array and be able to calculate the average grade of student in this sample array entry. I would appreciate if anyone can offer some solution on how to achieve that in this array.

String[] students = {"1,John,Smith, John1010@fakemail.com ,20,88,79,59",
                     "2,Suzan,Erickson, Sue9999@fakemail.com ,19,91,72,85",
                     "3,Jack,Napoli, Jack789@fakemail.com,19,85,84,87",
                     "4,Erin,Black, Aaron888@fakemail.com,22,91,98,82"};
shmosel
  • 49,289
  • 6
  • 73
  • 138
bencho
  • 23
  • 1
  • 9
  • 3
    Please do some research and read the docs. You will easily be able to find out how you split a String. – SamTebbs33 Jun 28 '16 at 22:38
  • 4
    http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java This link should give you an idea on how to split up a string in Java. – Module Jun 28 '16 at 22:41
  • google how to split strings in java and try your hands on. if you have difficulties then you come and paste your code with the problem you have. this is the only way we can help you. Nobody is going to write code for you here if you don't show us what you have tried so far. – Seek Addo Jun 28 '16 at 22:41
  • `String[] rows = students.split(",");`Then `String[] columns = rows[0].split(",");` to get the first row (John Smith's) values for instance. – robotlos Jun 28 '16 at 22:42
  • Do you need the average of each students or the total average? – Simon Jun 28 '16 at 22:43
  • @JonnyHenly Why haven't you? – shmosel Jun 28 '16 at 22:52
  • @Simon, yes I need the average of each student entry – bencho Jun 28 '16 at 22:52

3 Answers3

0

Here a piece of code that does what you want:

String[] students = ...
double[] averages = new double[students.length];

for (int i = 0; i < students.length; i++) {
    String[] student = students[i].split(",");
    int sum = 0;

    for (int j = 4; j < student.length; j++) {
        sum += Integer.parseInt(student[j]);
    }

    averages[i] = (double) sum / (student.length - 4);
}

The array averages will contain all the averages at the same position. Note that this code doesn't handle any wrong format and that it's assuming that all the remaining values are grades.

Simon
  • 774
  • 4
  • 21
-1

I guess writing code for you will not be so appropriate... However, I can suggest what needs to be done: 1. Split each string by using String class split(regex). You can simply split the string with comma. (https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)) 2. Just add the grade and divide by the size of students array.

I am sorry if you were expecting someone would provide the code...but learning by doing!!

Wantastic
  • 39
  • 5
-2
public static void main(String[] args) {
    int grades = 0;
    String[] students = { "1,John,Smith,John1010@fakemail.com,20,88,79,59",
            "2,Suzan,Erickson,Sue9999@fakemail.com ,19,91,72,85", "3,Jack,Napoli,Jack789@fakemail.com,19,85,84,87",
            "4,Erin,Black,Aaron888@fakemail.com,22,91,98,82" };
    for (int i = 0; i < students.length; i++) {
        String[] student = students[i].split(",");
        for (int j = 4; j < student.length; j++) {
            grades += Integer.parseInt(student[j]);

        }
        grades=0;System.out.println(student[2] + ": " +(double) grades / (student.length - 4));
    }
}
nunoymr
  • 16
  • 3
  • @Nunomr, Thanks, I can see from your code exactly where i made the mistake in my initial code. – bencho Jun 28 '16 at 22:57