我目前正在創建一個代碼,該代碼具有將繼承超類的數據字段和方法的子類。該子類也將有一個額外的領域,但我想從一個領域開始。錯誤:找到合適的構造函數並找不到符號
我正在使用名爲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
這裏是我的輸入文件:
您沒有定義接受您傳遞參數的構造函數。看看你的構造函數,並確定你認爲哪一個應該被稱爲 – Kon
,那麼我應該修改哪一個來解決所有這些問題?使用HawaiiNativeForestBirdsWithMoreData取而代之? – Siegfraud245
構造函數告訴你它期望的是什麼參數,以及它們有多少。例如'HawaiiNativeForestBirds(String name,String scientificname,String color,Integer population)'。如果你傳入的東西不是'name','scientificName','color'或'population'(*按順序*),那麼你的代碼將不起作用。請注意錯誤消息中的行號,並查找您不瞭解的錯誤。 – nbrooks