Search code examples
javasortingarraylistcomparablecompareto

Sorting Arrraylist from an input file using Comparable/Compareto Methods


I am trying to create a program that reads an input file and outputs it three times. The last output showing all the values from the input and are sorted out using my compareTo method.

I am trying to sort out the population on my array list but it seems I have an error

Here is my code so far:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.lang.Comparable;  
public class TestingCode {
/**
* The main method starts the program
*
*@param args is the input file
   */

   public static void main(String[] args) {
   //error checking for commandline input
      if(args.length != 1){
         System.out.println("Please enter at least one input file into the argument.");
         //terminates the program if more than 1 is entered
         System.exit(1);
      }
   
   
      String csvFile = args[0];
      String line = "";
      String cvsSplitBy = ",";
   
      List<HawaiiNativeForestBirds>  listofBirds = new ArrayList<HawaiiNativeForestBirds>();
      try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
      
         while ((line = br.readLine()) != null) {
         
            // use comma as separator
            String[] bird = line.split(cvsSplitBy);
            HawaiiNativeForestBirds Hawaiinbird= new HawaiiNativeForestBirds(bird[0],bird[1],bird[2],Integer.valueOf(bird[3]));
            listofBirds.add(Hawaiinbird);
         }
      
      } 
      catch (IOException e) {
         e.printStackTrace();
      }
   // First display null values
      HawaiiNativeForestBirds[]  hbirds=new        HawaiiNativeForestBirds[listofBirds.size()];
      System.out.println("index   " + "    element   ");  
      int i=0;
      for (HawaiiNativeForestBirds hbird:hbirds){
         i++;
         System.out.println(i+"            "+hbird);
      }
    // Now display actual values
      hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]);
   
      System.out.println("index   " + "name   "+ "   Scientific Name     "+ "        Color     " +       "      Population");        
      i=0;
      for (HawaiiNativeForestBirds hbird:hbirds){
         i++;
         System.out.println(i+"   "+hbird.toString());
      }
      
    //method to sort the array list
    
      Collection.sort(listofBirds);  
      
    // Now display actual values but with the added changes to the data/values  
      hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]);
   
      System.out.println("index   " + "name   "+ "   Scientific Name     "+ "        Color     " +       "      Population");        
      i=0;
      for (HawaiiNativeForestBirds hbird:hbirds){
         i++;
         System.out.println(i+"   "+hbird.toString2());
      }
   
   
   } // end of main 
} //end of class 
/**
   * Class HawaiianNativeForestBirds stores and displays the data for each HawaiianTheme object
   * 
   * 
   */
  
 
class HawaiiNativeForestBirds implements java.lang.Comparable<HawaiiNativeForestBirds>{  
   private String name;
   private String scientificname;
   private String color;
   private Integer population;
   public HawaiiNativeForestBirds(){
   
   }
   //constructor - used to initialize the four data fields
      /**
   * Stores the name,scientific name, color and population of the Hawaiian Birds
    *  
    * 
    * @param name is the name of the birds from the array list
    * @param scientificname is the scientific name of the birds in the array list
    * @param color is the color of each of the birds in the array list
    * @param population is the total number of birds in the array list   
    */

   public HawaiiNativeForestBirds(String name, String scientificname,
        String color, Integer population) {
      super();
      this.name = name;
      this.scientificname = scientificname;
      this.color = color;
      this.population = population;
   }  
/**
    * Gets each bird's name
    * 
    * @return the birds name
    */


   public String getName() {
      return name;
   }
    /**
    * Sets each birds name
    * 
    * @param name is the bird's name
    */

   public void setName(String name) {
      this.name = name;
   }
   /**
    * Gets the bird's scientific name
    * 
    * @return the bird's scientific name
    */

   public String getScientificname() {
      return scientificname;
   }
    /**
    * Sets the birds scientific name
    * 
    * @param scientificname is the bird's scientific name
    */

   public void setScientificname(String scientificname) {
      this.scientificname = scientificname;
   }
   /**
    * Gets the bird's color
    * 
    * @return the bird's color
    */

   public String getColor() {
      return color;
   }
    /**
    * Sets the bird's color
    * 
    * @param color is the bird's color
    */

   public void setColor(String color) {
      this.color = color;
   }
   /**
    * Gets the bird's population
    * 
    * @return total population of the bird
    */

   public Integer getPopulation() {
      return population;
   }
    /**
    * Sets the bird's population
    * 
    * @param population is the total population of the bird
    */

   public void setPopulation(Integer population) {
      this.population = population;
   }
  /**
    * Display the output 
    * 
    * @return the name, scientificname, color, and population of the bird
    */
   
        
   public String toString() {
      String output = name +"     "+      scientificname + "             "+ color +"           "+        population;
      return output;
   }//end of toString 
   
   //compareTo method that sorts the population
   public int compareTo(HawaiiNativeForestBirds comparestu) {
      int comparePopulation=((HawaiiNativeForestBirds)comparestu).getPopulation();
        /* For Ascending order*/
      return this.population-comparePopulation;
   
   } //end of compareTO     
   
   
   /**
    * Display the outputs with changed elements/values
    * 
    * @return the name, scientificname, color, and population of the bird but with changes made
    * to the data.
    */
   
   public String toString2() {
      population = population + 1;
      String output = name.toUpperCase() +"     "+      scientificname.toUpperCase() + "             "+ color.toUpperCase() +"           "+        population;
      return output;
   }//end of toString2        
}// end of class

Hopefully I did my comparable method correctly and the only issue now is my method that calls the sorting method.

Error:

TestingCode.java:72: error: cannot find symbol
      Collection.sort(listofBirds);  
      ^
  symbol:   variable Collection
  location: class TestingCode
1 error

my inputfile:

birds1.csv

EDIT: added my input file sorry!


Solution

  • Two things:

    • The compiler can't find the Collection class. That's due to the lack of appropriate import.
    • The sort() method is in the Collections class, not the Collection class.