2016-09-27 91 views
-2

我目前正在創建一個代碼,該代碼具有將繼承超類的數據字段和方法的子類。該子類也將有一個額外的領域,但我想從一個領域開始。錯誤:找到合適的構造函數並找不到符號

我正在使用名爲birds.csv的輸入文件,它具有4列。我想添加第5個coumn,我已經做了10行數據。

我正在使用該子類來獲取和設置字段的方法並初始化它。

我目前有4個錯誤與我的代碼,我真的需要幫助我需要修復。

代碼:

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

public class TestingCode { 

    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 HawaiiNativeForestBirdsWithMoreData(bird[0],bird[1],bird[2],Integer.valueOf(bird[3]),bird[4]); 
      listofBirds.add(Hawaiinbird); 
     } 
     } 
     catch (IOException e) { 
     e.printStackTrace(); 
     } 

     HawaiiNativeForestBirds[] hbirds=new  HawaiiNativeForestBirds[listofBirds.size()]; 
     System.out.println("index " + " element "); 
     int i=0; 
     for (HawaiiNativeForestBirds hbird:hbirds){ 
     i++; 
     System.out.println(i+"   "+hbird); 
     } 

     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()); 
     } 

     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()); 
     } 

     hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]); 

     System.out.println("index " + "name "+ " Scientific Name  "+ "  Color  " +  "  Population" +  "  Author");   
     i=0; 
     for (HawaiiNativeForestBirds hbird:hbirds){ 
     i++; 
     System.out.println(i+" "+hbird.toString3()); 
     } 
    } 
} 

class HawaiiNativeForestBirds { 
    protected String name; 
    protected String scientificname; 
    protected String color; 
    protected Integer population; 

    public HawaiiNativeForestBirds(){ 
    } 

    public HawaiiNativeForestBirds(String name, String scientificname, 
     String color, Integer population) { 
     super(); 
     this.name = name; 
     this.scientificname = scientificname; 
     this.color = color; 
     this.population = population; 
    } 

    // getters and setters hidden 

    public String toString() { 
     String output = name + "  " + scientificname + "    " + color + "   " + population; 
     return output; 
    } 

    public String toString2() { 
     population = population + 1; 
     String output = name.toUpperCase() + "  " + scientificname.toUpperCase() + "    "+ color.toUpperCase() + "   " + population; 
     return output; 
    } 
} 

HawaiiNativeForestBirdsWithMoreData

class HawaiiNativeForestBirdsWithMoreData extends HawaiiNativeForestBirds { 

    private String author; 

    public HawaiiNativeForestBirdsWithMoreData(){ 
    } 

    public HawaiiNativeForestBirdsWithMoreData(String name, String scientificname, 
     String color, Integer population, String author) { 
     super(name, scientificname, color, population); 
     this.author = author; 
    } 

    public String getAuthor() { 
     return author; 
    } 

    public void setAuthor(String author) { 
     this.author = author; 
    } 

    public String toString3() { 
     population = population + 1; 
     String output = name.toUpperCase() + "  " + scientificname.toUpperCase() + "    " + color.toUpperCase() + "   " + population + "   " +author.toUpperCase(); 
     return output; 
    } 
} 

這裏是我的錯誤:

TestingCode.java:84: error: cannot find symbol 
     System.out.println(i+" "+hbird.toString3()); 
             ^
    symbol: method toString3() 
    location: variable hbird of type HawaiiNativeForestBirds 
1 error 

這裏是我的輸入文件: birds.csv

+0

您沒有定義接受您傳遞參數的構造函數。看看你的構造函數,並確定你認爲哪一個應該被稱爲 – Kon

+0

,那麼我應該修改哪一個來解決所有這些問題?使用HawaiiNativeForestBirdsWithMoreData取而代之? – Siegfraud245

+0

構造函數告訴你它期望的是什麼參數,以及它們有多少。例如'HawaiiNativeForestBirds(String name,String scientificname,String color,Integer population)'。如果你傳入的東西不是'name','scientificName','color'或'population'(*按順序*),那麼你的代碼將不起作用。請注意錯誤消息中的行號,並查找您不瞭解的錯誤。 – nbrooks

回答

1

問題可能不在於你的構造函數是如何聲明鳥的實例的。你有,構造函數爲(String,String,String,Int,String),但是你的數據是按順序(String,String,Int,String)。仔細檢查您的csv文件中的訂單,並確保它與您在參數中傳遞的順序相匹配。

編輯:檢查csv文件後。人口是在列表中的第4項,所以

HawaiiNativeForestBirds Hawaiinbird= new HawaiiNativeForestBirds(bird[0],bird[1],Integer.valueOf(bird[2]), bird[3]); 

此外,如所指出的,存在,所以你需要更新,以適應它的構造函數傳遞一個第五個參數。

編輯爲最後一個錯誤:

陣列的數據類型不使用toString3()方法多什麼是必要的。即使實際類型包含toString3(),只有類型爲HawaiiNativeForestBirds的用戶才能訪問toString()和toString2()。

+0

構造函數需要4個參數。你正在傳遞5個參數,就像OP一樣。這是第一個問題。 – nbrooks

+0

是的,我想通過5個參數 – Siegfraud245

+0

好吧!我設法解決了兩個減少所有問題並解決大部分問題的問題。仍然只有1個錯誤!我在頂部 – Siegfraud245