Error: suitable constructor found and cannot find symbol
Error: suitable constructor found and cannot find symbol
我目前正在创建一个具有子类的代码,该子类将继承超类的数据字段和方法。子类还将有一个附加字段,但我想从一个字段开始。
我正在使用一个名为 birds.csv 的输入文件,它有 4 列。我想添加第 5 个列,其中包含我已经完成的 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;
}
}
Class 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
这是我的输入文件:
问题可能不在于您的构造函数,因为您如何声明鸟的实例。你有,构造函数为(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]);
此外,正如所指出的那样,传入了第 5 个参数,因此您需要更新构造函数以适应它。
编辑最后一个错误:
数组的数据类型与使用toString3()方法所需要的不多。当它属于 HawaiiNativeForestBirds 类型时,您将只能访问 toString() 和 toString2(),即使实际类型包含 toString3()。
我目前正在创建一个具有子类的代码,该子类将继承超类的数据字段和方法。子类还将有一个附加字段,但我想从一个字段开始。
我正在使用一个名为 birds.csv 的输入文件,它有 4 列。我想添加第 5 个列,其中包含我已经完成的 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;
}
}
Class 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
这是我的输入文件:
问题可能不在于您的构造函数,因为您如何声明鸟的实例。你有,构造函数为(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]);
此外,正如所指出的那样,传入了第 5 个参数,因此您需要更新构造函数以适应它。
编辑最后一个错误:
数组的数据类型与使用toString3()方法所需要的不多。当它属于 HawaiiNativeForestBirds 类型时,您将只能访问 toString() 和 toString2(),即使实际类型包含 toString3()。