Siegfraud245 Siegfraud245 - 2 months ago 7
Java Question

Error: suitable constructor found and cannot find symbol

I am currently creating a code that has a subclass which will inherit the data fields and methods of a superclass. The subclass will also have an additional field but I wanted to start with one field.

I am using an input file called birds.csv that has 4 columns. I want to add 5th coumn with 10 rows of data which I already did.

I am using that subclass to get and set the methods of the field and initialize it.

I currently have 4 errors with my code and I really need help with what I need to fix.

Code:

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


Here are my errors:

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


Here is my input file :
birds.csv

Answer

The problem may not be with your constructor by how you declared the instance of a bird. You have, the constructor as (String, String, String, Int, String), but your data is in the order (String, String, Int, String). Double check the order in your csv file and make sure it matches the order in which you are passing in the parameters.

Edit: After checking the csv file. the population is the 4th item in the list, so

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

Also, as pointed out, there is a 5th parameter being passed in so you'll need to update the constructor to accommodate for it.

Edit for last error:

The data type of the array does not much what is needed to use the toString3() method. You'll only have access to toString() and toString2() while it is of type HawaiiNativeForestBirds even if the actual type contains toString3().