2016-09-30 2 views
-4

J'essaie de créer un programme qui lit un fichier d'entrée et le sort trois fois. La dernière sortie affiche toutes les valeurs de l'entrée et est triée en utilisant ma méthode compareTo.Trier Arrrylist à partir d'un fichier d'entrée en utilisant des méthodes comparables/comparées

Je suis en train de trier la population sur ma liste de tableau, mais il semble que j'ai une erreur

Voici mon code à ce jour:

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

Espérons que je l'ai fait ma méthode comparable correctement et le seul problème est maintenant ma méthode qui appelle la méthode de tri.

Erreur:

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

mon inputfile:

birds1.csv

EDIT: ajouté mon fichier d'entrée désolé!

+2

Eh bien, il n'y a pas de méthode sort() dans la classe Collection. C'est en classe 'Collections'. Lisez le javadoc. –

+0

Veuillez noter la classe 'collections' dans le paquetage java.util'. pas 'collection' –

Répondre

0

Deux choses:

  • Le compilateur ne peut pas trouver la classe Collection. C'est en raison du manque de import approprié.
  • La méthode sort() se trouve dans la classe Collections et non dans la classe Collection.
+0

oh frick! je l'ai ! – Siegfraud245