2016-09-30 1 views
-4

入力ファイルを読み込んで3回出力するプログラムを作成しようとしています。最後の出力は入力からのすべての値を示し、compareToメソッドを使用してソートされます。Comparable/Comparetoメソッドを使用して入力ファイルからArrraylistをソートする

私は配列リストに人口を整理しようとしていますが、私のコードは、これまでのところで、私はここでエラー

を持っているようだ:

うまくいけば、私がやった

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

私の比較可能なメソッドが正しく、唯一の問題は今ソートメソッドを呼び出す私のメソッドです。

エラー:

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

私の入力ファイル:

birds1.csv

EDIT: は私の入力ファイルは申し訳ありません追加!

+2

まあ、Collectionクラスにsort()メソッドはありません。クラス 'Collections'にあります。 javadocを読んでください。 –

+0

java.util'パッケージの 'collections'クラスに注意してください。コレクションではありません –

答えて

0

2つのこと:

  • コンパイラはCollectionクラスを見つけることができません。それは適切なimportの欠如によるものです。
  • sort()メソッドはCollectionsクラスであり、Collectionクラスではありません。
+0

ああああ!とった ! – Siegfraud245

関連する問題