title: How do I find the name of the top student in an array list?
body: <p>I am studying Java and have been asked to produce methods that can be used in order to gather statistics based off of the student names and marks that are entered. I have worked out how to calculate the top mark but what I need to do is return the name of the student that got the highest mark, how would I do this? I was thinking I could try to return the string before the highest int but I wasn't sure how I would do that. </p>
  
  <p>Edit: Just to make it clear, currently, when END is entered in to the console following the input of data, the top mark is returned - I need to return the mark of the best student.</p>
  
  <pre><code>import java.util.*;
  
  public class Course {
      private ArrayList&lt;Student&gt; people = new ArrayList&lt;Student&gt;();
      private int passing = 0;
      private int failing = 0;
      private int top = Integer.MIN_VALUE;
      private int sum = 0;
  
      public void add( Student student ) {
          people.add( student );
          if(student.getMark() &gt;= 40){
              passing++;
          }
          else {
              failing++;
          }
          sum += student.getMark();
          if(student.getMark() &gt; top) {
              top = student.getMark();
          }
      }
  
      public int pass() {
          return passing;
      }
  
      public int fail() {
          return failing;
      }
  
      public int top() {
          return top;
      }
  
      public double average() {
          return sum / people.size();
      }
  }
  </code></pre>
  
  <p>Thanks for any help.</p>
  
  <p>Update: BinaryJudy, I did what you said but I get a 'NoSuchMethod' error for the top name, this is what I changed my code to:</p>
  
  <pre><code>import java.util.*;
  
  public class Course {
      private ArrayList&lt;Student&gt; people = new ArrayList&lt;Student&gt;();
      private int passing = 0;
      private int failing = 0;
      private int top = Integer.MIN_VALUE;
      private int sum = 0;
      private String topName;
  
      public void add( Student student ) {
          people.add( student );
          if(student.getMark() &gt;= 40){
              passing++;
          }
          else {
              failing++;
          }
          sum += student.getMark();
          if(student.getMark() &gt; top) {
              top = student.getMark();
          }
  
          if(student.getMark() &gt; top) {
              top = student.getMark();
              topName = student.getName();
          }
      }
  
      public int pass() {
          return passing;
      }
  
      public int fail() {
          return failing;
      }
  
      public String top() {
          return topName;
      }
  
      public double average() {
          return sum / people.size();
      }
  }
  </code></pre>
  
  <p>Any idea why? :)</p>