0

So this is the question:

Your program should be able to read from a file the above data into an array and sort the array by the student’s name in an ascending order. Selection sort algorithm is strongly recommended on the array of objects. And also the record office would like to have a sorted class list for each class, freshman, sophomore, junior, and senior.

Print out the following:

  1. The sorted master list with the average GPA of the entire college.
  2. The freshman list with the average GPA of the freshmen.
  3. The sophomore list with the average GPA of the sophomores.
  4. The junior list with the average GPA of the juniors.
  5. The senior list with the average GPA of the seniors.

So far I'm not able to get my list to sort itself. Every time I try to compile, it gives me the exception:

Exception in thread "main" java.lang.NullPointerException
    at Project1.main(Project1.java:33)

Here is my code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Project1 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader =
            new BufferedReader(new FileReader("/Users/Ayesha/Desktop/inputfile.txt"));
        ArrayList<String> list = new ArrayList<>();
        String data[] = new String[25];
        while (true) {
            String line = reader.readLine();
            for (int i = 0; i < 24; i++) {
                data[i] = line;
            }
            if (line == null) {
                break;
            }
            System.out.println(line);
            list.add(line);
        }

        // String data[]=list.toArray(new String[25]);

        reader.close();

        for (int i = 0; i < data.length - 1; ++i) {
            int minIndex = i;
            for (int j = i + 1; j < data.length; ++j) {
                if (data[j].compareTo(data[minIndex]) < 0) {
                    minIndex = j;
                }
            }
            String temp = data[i];
            data[i] = data[minIndex];
            data[minIndex] = temp;
        }
        System.out.println(data);
    }
}

Here is the source file:

NAME  CLASS   GPA
Williams, Leonard Freshman    1.85
Smith, Sheila Senior  2.99
Anderson, Andy    Sophomore   3.01
Wiser, Bud    Freshman    4.00
Robertson, Jully  Junior  2,78
Koran, Korn   Junior  3.50
Smith, Sam    Junior  2.14
Johnson, Jim  Junior  3.05
Johnson, Jane Junior  3.75
Potter, Pam   Senior  2.98
Brown, Bill   Sophomore   2.55
Crooks, Cathy Freshman    1.99
Gregg, Howard Senior  2.44
Nicholas, Judy    Senior  3.69
White, Bob    Sophomore   1.64
Walsh, Fred   Junior  4.00
Dennis, Susan Senior  2.06
Roberts, Rachel   Sophomore   4.00
Fredericks, Mary  Freshman    2.89
Holmes, Wendy Senior  2.56
Edwards, James    Sophomore   3.00
Green, Barbara    Sophomore   3.67
Brown, David  Freshman    2.00
Williamson, Walt  Sophomore   2.95
Carson, Jim   Sophomore   2.03
Tom
  • 16,842
  • 17
  • 45
  • 54
Ayesha S
  • 21
  • 5
  • 1
    I doubt you get an exception during compilation - did you mean execution? What exception? Could you post the stacktrace? – Kenney Oct 03 '15 at 16:54
  • Could you please give samples of your input file ? – Afsin Buyuksarac Oct 03 '15 at 16:55
  • Can you edit your question and include the exact exception you are getting? Since you are saying "when I try to compile", does the compiler give you an error, or do you actually get an exception at runtime? – bgse Oct 03 '15 at 16:56
  • I suggest that you also look into java Collections.sort, this function can do all the sorting of your list as you wish just by passing a Comparator – Petter Friberg Oct 03 '15 at 16:57
  • 1
    First correct your List definition with `ArrayList list = new ArrayList();` – Afsin Buyuksarac Oct 03 '15 at 16:57
  • I edited my code with the exact exception and the input file – Ayesha S Oct 03 '15 at 17:03
  • I also don't know how exactly to access the gpa elements from my array so if you could explain that to me as well that would be great! – Ayesha S Oct 03 '15 at 17:06
  • At the moment, you have the line as a simple string in your array. You could split based on blanks, e.g. something like line.split(" ") and access the individual elements. Look here: http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – bgse Oct 03 '15 at 17:32
  • Ideally, you'd probably create a class Student that has name, firstname and gpa as separate fields, and insert Student-Objects into your data array while you read the input file. – bgse Oct 03 '15 at 17:37

2 Answers2

0
    while (true) {
        String line = reader.readLine();
        for(int i=0; i<24; i++){
            data[i]= line;
        }
        if (line == null) {
            break;
        }
        System.out.println(line);
        list.add(line);
    }   

in this code snippet, you're actually reading a line from your file and inserting that same line into all 24 of your 25 slots of String data[]= new String[25];. That might be a bug; you should be asking the for loop to run while it i is less than 25.

More importantly, you meant to read one line and insert that one line into one dataslot, then move on the the next. Because for a file that may not contain sufficient lines, let's say it only has 7 lines. When you're reading line 8, which your filereader translates as null, all of the Strings in data[] will be null.

user2651804
  • 1,464
  • 4
  • 22
  • 45
-1

This is improved code of your solution,

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Project1 {
    public static void main(String[] args) throws IOException {

        BufferedReader reader = new BufferedReader(new FileReader("/Users/Ayesha/Desktop/inputfile.txt"));
        ArrayList<String> list = new ArrayList<String>();
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
            list.add(line);
        }

        String data[] = list.toArray(new String[list.size()]);

        reader.close();

        for (int i = 0; i < data.length - 1; ++i) {
            int minIndex = i;
            for (int j = i + 1; j < data.length; ++j) {
                if (data[j].compareTo(data[minIndex]) < 0) {
                    minIndex = j;
                }
            }
            String temp = data[i];
            data[i] = data[minIndex];
            data[minIndex] = temp;
        }

       // This is how you get each gpa for each line.   
       for (int i = 0; i < data.length; i++) {
          String[] words = data[i].split(" ");
          String gpa = words[3];
          System.out.println(gpa);
       }

    }
}
Afsin Buyuksarac
  • 298
  • 1
  • 10
  • 1
    Could you be a bit more verbose with comments here? The asker is apparently trying to solve a question for school or college, and it does not help them if they don't understand what's happening. – bgse Oct 03 '15 at 17:40
  • Thanks! You literally saved my life!! Can you explain the gpa thing? i don't really get what's going on there.. – Ayesha S Oct 03 '15 at 17:42
  • Hello Ayesha, since for one line you have 4 words. Simply I split one line with "space" character. This would give you 4 words. 4th of the words would be GPA grade that you want. – Afsin Buyuksarac Oct 03 '15 at 17:43
  • @bgse I was in a rush that's why I just copied the code here. Ayesha asked the solution for description and I did, is it OK ? – Afsin Buyuksarac Oct 03 '15 at 17:44
  • So my System.out.println(gpa) is still giving me an out of bounds exception.. Do I need to go System.out.println(gpa[i])? – Ayesha S Oct 03 '15 at 17:49
  • @bgse About your suggested edit: the comment `words will now contain elements, e.g. ["John,", "Smith", "Freshman", "3.09"]` is misleading, because the result of this `split` here returns a larger array with more elements then just these (from a single line). – Tom Oct 03 '15 at 18:00
  • So how would I split the array to only access the gpa or the class?? – Ayesha S Oct 03 '15 at 18:30
  • @AyeshaS Fix check if your text file really has multiple whitespaces to delimit the parts. If yes, then use `"\\s+"` instead of `" "`. – Tom Oct 03 '15 at 18:36
  • @AyeshaS, are you using headers also ? You may not use them in your input file. – Afsin Buyuksarac Oct 03 '15 at 18:37
  • My code right now looks like this: for (int i = 0; i < data.length; i++) { String[] words = data[i].split("\\s+"); // String gpa = words[i]; System.out.println(words[i]); And the output is:Andy Sophomore 2.00 – Ayesha S Oct 03 '15 at 19:03
  • So you print 4th element words[3]. This is GPA. – Afsin Buyuksarac Oct 03 '15 at 19:44
  • @AfsinBuyuksarac When I try to print that it gives me an out of bounds exception. – Ayesha S Oct 03 '15 at 19:55
  • Is that throwing at first line ? – Afsin Buyuksarac Oct 03 '15 at 20:02
  • It's not able to understand element word[3] at all. – Ayesha S Oct 03 '15 at 20:17
  • @AfsinBuyuksarac I've tried to change my code to String[]words=new String[25]; for (int i = 0; i < 24; i++) { for (int j=0; j<24; j++) { words[j] = data[i].split(":"); String gpa = words[i]; System.out.println(words[i]); But the 3rd to last line is not working. how do I fix this?? – Ayesha S Oct 03 '15 at 20:18
  • Ayesha, your input file contains lines and some words right ? We can call lines as rows, I mean data. And then we can call words as columns seperated by space character. Then for the array or words ["John,", "Smith", "Freshman", "3.09"] has 4 elements. The 4th element is GPA right ? In java we can get this words[3]. For each line (data) we have words of 4 element. We have 25 data lines. And 4 words each line 25 * 4 = 100 words right ? – Afsin Buyuksarac Oct 03 '15 at 20:27
  • While iterating over data, every 4th element of words is GPA which means as in my ANSWER, `for (int i = 0; i < data.length; i++) { String[] words = data[i].split("\\s"); String gpa = words[3]; // THIS IS GPA System.out.println(gpa); }` – Afsin Buyuksarac Oct 03 '15 at 20:27