0

The homework requires reading two file and entering the information of students into two separate ArrayLists. Then I need to perform 2 functions: combine the two lists and sort the combined list.

public abstract class Tools {
    public static ArrayList<JUPASStudent> readJUPASFile(String f) throws Exception {
        ArrayList<JUPASStudent> jList = new ArrayList<JUPASStudent>();

        BufferedReader readbuffer = null;
        readbuffer = new BufferedReader(new FileReader(f));
        String strRead;
        while ((strRead = readbuffer.readLine()) != null) {
            String splitarray[] = strRead.split("/t");
            String firstentry = splitarray[0];
            String secondentry = splitarray[1];
            JUPASStudent x = new JUPASStudent(firstentry, Double.parseDouble(secondentry));
            jList.add(x);
        }

        readbuffer.close();
        return jList;
        }

    public static ArrayList<NonJUPASStudent> readNonJUPASFile(String f) throws Exception {
        ArrayList<NonJUPASStudent> njList = new ArrayList<NonJUPASStudent>();

        BufferedReader readbuffer = null;
        readbuffer = new BufferedReader(new FileReader(f));
        String strRead;
        while ((strRead = readbuffer.readLine()) != null) {
            String splitarray[] = strRead.split("/t");
            String firstentry = splitarray[0];
            String secondentry = splitarray[1];
            NonJUPASStudent x = new NonJUPASStudent(firstentry, Double.parseDouble(secondentry));
            njList.add(x);
        }

        readbuffer.close();
        return njList;
    }

    public static ArrayList<Student> combineArrayList(ArrayList<JUPASStudent> S1, ArrayList<NonJUPASStudent> S2) {
        ArrayList<Student> sList = new ArrayList<Student>();

        for (int i = 0; i < S1.size(); i++)
            sList.add(S1.get(i));
        for (int i = 0; i < S2.size(); i++) 
            sList.add(S2.get(i));

        return sList;
    }

    public static ArrayList<Student> sort(ArrayList<Student> s){
        for (int i = 0; i < s.size()-1; i++) {
            for (int j = 0; i < s.size()-i-1; j++) {
                if (s.get(j).getResult() > s.get(j+1).getResult()) {
                    Student Temp = s.get(j);
                    s.set(j, s.get(j+1));
                    s.set(j+1, Temp);
                }
            }
        }
        return s;
    }
}

However, I keep getting "Index 1 out of bounds for length 1"

Andronicus
  • 25,419
  • 17
  • 47
  • 88
Caspar Ng
  • 11
  • 2
  • Welcome to SO. Please read [ask] and note that when asking about exceptions you should post the stacktrace and mark the lines of your code mentioned there. In short the message you're getting means that you're trying to access index 1 of an array that only has index 0 (i.e. length 1). – Thomas Oct 17 '19 at 06:07
  • Please post the stack trace of the error. – Sujay Mohan Oct 17 '19 at 06:32

4 Answers4

2

In the inner loop you're defining a constraint to the wrong counter. Instead of :

for (int j = 0; i < s.size()-i-1; j++)

there should be:

for (int j = 0; j < s.size()-i-1; j++)
Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • 1
    Good spot. One more example for why more meaningful variable names help reduce bugs that result from typos (or confusing variables). – Thomas Oct 17 '19 at 06:09
  • thanks, I just amended the code but the problem remains unresolved. – Caspar Ng Oct 17 '19 at 07:01
1

In package java.util, we can :

  • Collections.sort(List); // to sort list, ex:

Collections.sort(Arrays.asList(13, 4));

  • Collections.addAll(Collection c, T... elements); // to join list, ex :

Collections.addAll(Arrays.asList(13, 4), Arrays.asList(3,4,5));

So you don't need to write your own function anymore.

sovannarith cheav
  • 743
  • 1
  • 6
  • 19
0

Can you add few system.out statements. It will help you see where the error. I suspect data from file.

0

The issue you are facing might be in the below line. in method readNonJUPASFile and readJUPASFile

String secondentry = splitarray[1];

You are splitting the String based on /t. If you are expecting to split it based on tab you should be giving \t. If your line does not contain /t as you have given you will probably get a ArrayIndexOutOfBoundsException. You should check the length before assigning the variable like

if (splitarray.length > 1) {
   String firstentry = splitarray[0];
   String secondentry = splitarray[1];
}
Sujay Mohan
  • 933
  • 7
  • 14