title: Using Comparable's compareTo to compare an object to the elements in an arraylist
body: <p>I created an object Student using Comparable with getters/setters as well as a method that overrides compareTo. In a separate file an arraylist of objects is populated from a text file. Now I need to compare the values in the arraylist to another Student object.</p>
  
  <p>The file was used to create an arraylist as below:</p>
  
  <pre><code>    try {
      private static ArrayList&lt;Student&gt; array = new ArrayList&lt;Student&gt;();
      File file = new File("students.txt");
      Scanner scanner = new Scanner(file);
  
      while (scanner.hasNextLine()) {                
          String inline = scanner.nextLine();
          String[] split = inline.split(":");
          Student myStudent  = new Student();
          myStudent.setUsername(split[0]);
          myStudent.setPassword(split[1]);
  
          array.add(myStudent);
  
      }
      scanner.close();
      }
      catch (FileNotFoundException e)
      {
      System.out.println("ERROR.");
      }
  </code></pre>
  
  <p>The text file looks like this:</p>
  
  <p>John:password1</p>
  
  <p>Jane:password2</p>
  
  <p>Jack:password3</p>
  
  <p>(One on each line, no blank lines in between.)</p>
  
  <p>And in a separate method a created Student object is compared to the elements in the arraylist:</p>
  
  <pre><code>    Student aStudent = new Student();
      aStudent.setUsername("student");
      aStudent.setPassword("password");
      boolean found = false;
      for (int i = 0; i &lt; array.size(); i++)
      {
      if (array.get(i).compareTo(aStudent) == 0)
      {
        System.out.println(aStudent.equals(array.get(i)));
        found = true;
        break;
      }
      else
      {
        System.out.println("No such records found!");
        found = false;
        break;
      }
      System.out.println(found);
      }
  </code></pre>
  
  <p>The problem is that the object aStudent is not being compared with the objects in the arraylist. It does not print out anything (a -1, 0, or 1) for the call to compareTo, but it always shows that found is true, even though it should be false when there are no matches for aStudent in the file (which there aren't any matches to the username "student" or the password "password").</p>
  
  <p>All together my code complies and works - it just works incorrectly.</p>
  
  <p>Sorry if this sounds confusing. In short, my question is how can I compare the objects of an arraylist to another object using the Comparable interface and compareTo? A plus is if you can tell me what I'm doing wrong.</p>
  
  <p>Thank you in advance.</p>
  
  <p>EDIT</p>
  
  <p>Here is the overriding of the compareTo method:</p>
  
  <pre><code>    public int compareTo(Student obj){
      int result = 1;
      if ((this.Username.compareToIgnoreCase(object.Username) &lt; 0) || (this.Password.compareTo(object.Password) &lt; 0))
      {
        result = -1;
      }
      else if ((this.Username.compareToIgnoreCase(object.Username) == 0) &amp;&amp; (this.Password.compareTo(object.Password) == 0))
      {
        result = 0;
      }
  
      return result;
  
      }
  </code></pre>