В настоящее время я создаю код с подклассом, который наследует поля данных и методы суперкласса. Подкласс также будет иметь дополнительное поле, но я хотел бы начать с одного поля.Ошибка: подходящий конструктор обнаружен и не найден символ
Я использую файл ввода, называемый 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;
}
}
Класс 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 sciencename, String color, Integer population)'. Если вы передаете что-то, что не является «именем», «научным именем», «цветом» или «населением» (* в этом порядке *), то ваш код не будет работать. Обратите внимание на номера строк в сообщениях об ошибках и найдите ошибки, которые вы не понимаете. – nbrooks