title: How do I compare and order two arraylists in Java
body: <p>I am getting input from two files on my hard drive:</p>
  
  <p>studentNames.txt and studentScores.txt - the names have the student ID and name while the scores has the student ID and scores. I have put the data into two ArrayLists and want to sort the data so that the grades go to the matching ID.</p>
  
  <p>For example:</p>
  
  <pre><code>+------+--------------+---------+
  |  ID  |     Name     |  Grade  |
  +------+--------------+---------+
  | 3305 | Smith Henry  | 92.0    |
  | 5555 | Eddy Olivia  | 95.5    |
  | 8915 | Johnson Luke | 98.5    |
  +------+--------------+---------+
  </code></pre>
  
  <p>And the data continues to populate with just the ID / Grade - I know I need to use an if statement, but how would I go about doing that?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  public class P_Supplemental_9 {
  
  public static void main(String[] args) throws FileNotFoundException {
     File file1 = new File("c:/temp/studentNames.txt");
     File file2 = new File("c:/temp/studentScores.txt");
  
     if(file1.exists()) {
     Scanner input = new Scanner(file1);
  
     ArrayList&lt;Student&gt; students = new ArrayList();
  
     while(input.hasNext()) {
     students.add(new Student(input.nextInt(),input.nextLine()));
     }
  
     input.close();
  
     for(int o = 0;o &lt; students.size();o++) {
     System.out.printf("%10d %20s avg\n", students.get(o).getStuId(),     students.get(o).getStuName());
  
     } // end for
  
     }
  
     if(file2.exists()) {
         Scanner input = new Scanner(file2);
  
         ArrayList&lt;Student&gt; grades = new ArrayList();
  
         while(input.hasNext()) {
             grades.add(new Student(input.nextInt(), input.nextLine()));
  
         } /// end while
         input.close();
  
     for(int o = 0;o &lt; grades.size();o++) {
  
         System.out.printf("%10d %20s avg\n", grades.get(o).getStuId(), grades.get(o).getStuName());
     } // end for
  
     } // end if(file2.exists)
  
  
  
  
  
    } // end main method
   } // end P_Supplemental_9
  
  
  
  class Student {
      private int stuId;
      private String stuName;
      private ArrayList &lt;Double&gt; grades;
  
      Student(int idIn, String nameIn) {
  
          this.stuId = idIn;
          this.stuName = nameIn;
         } // end student class
  
      Student(int idIn, ArrayList gradesIn) {
          this.stuId = idIn;
          this.grades = gradesIn;
  
      }
  
          public int getStuId() {
              return stuId;
          }
  
          /**
           * @param stuId the stuId to set
           */
          public void setStuId(int stuId) {
              this.stuId = stuId;
          }
  
          /**
           * @return the stuName
           */
          public String getStuName() {
              return stuName;
          }
  
          /**
           * @param stuName the stuName to set
           */
          public void setStuName(String stuName) {
              this.stuName = stuName;
          }
  
          /**
           * @return the grades
           */
          public ArrayList getGrades() {
              return grades;
          }
  
          /**
           * @param grades the grades to set
           */
          public void setGrades(ArrayList grades) {
              this.grades = grades;
          }
  
  } // end student class
  </code></pre>
  
  <p>Heres the data from Studentnames.txt</p>
  
  <pre><code>3305 Smith Henry
  5555 Eddy Olivia
  8915 Johnson Luke
  </code></pre>
  
  <p>Heres the data from Studentscores.txt</p>
  
  <pre><code>3305 92.0
  5555 95.5
  8915 98.5
  3305 89.0
  5555 90.5
  8915 95.5
  3305 78.5
  5555 85.0
  8915 82.0
  </code></pre>