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:
- The sorted master list with the average GPA of the entire college.
- The freshman list with the average GPA of the freshmen.
- The sophomore list with the average GPA of the sophomores.
- The junior list with the average GPA of the juniors.
- 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