[英]Error: suitable constructor found and cannot find symbol
我目前正在創建一個具有子類的代碼,該子類將繼承超類的數據字段和方法。 子類還將有一個額外的字段,但我想從一個字段開始。
我正在使用一個名為 Bird.csv 的輸入文件,它有 4 列。 我想用我已經做過的 10 行數據添加第 5 行。
我正在使用該子類來獲取和設置字段的方法並對其進行初始化。
我的代碼目前有 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
這是我的輸入文件:
問題可能不在於你的構造函數你如何聲明一只鳥的實例。 你有,構造函數為(字符串,字符串,字符串,整數,字符串),但你的數據是按順序排列的(字符串,字符串,整數,字符串)。 仔細檢查 csv 文件中的順序,確保它與您傳入參數的順序相匹配。
編輯:檢查 csv 文件后。 人口是列表中的第 4 項,所以
HawaiiNativeForestBirds Hawaiinbird= new HawaiiNativeForestBirds(bird[0],bird[1],Integer.valueOf(bird[2]), bird[3]);
此外,正如所指出的,傳入了第 5 個參數,因此您需要更新構造函數以適應它。
編輯最后一個錯誤:
使用 toString3() 方法所需的數組數據類型並不多。 您只能訪問 toString() 和 toString2(),而它的類型是 HawaiiNativeForestBirds,即使實際類型包含 toString3()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.