错误:找到合适的构造函数,找不到符号

时间:2016-09-27 20:28:11

标签: java arrays inheritance subclass

我目前正在创建一个具有子类的代码,该子类将继承超类的数据字段和方法。子类还有一个额外的字段,但我想从一个字段开始。

我正在使用名为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

这是我的输入文件: birds.csv

1 个答案:

答案 0 :(得分:1)

问题可能不在于您的构造函数是如何声明鸟的实例的。你有,构造函数为(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()方法所需的数组类型并不多。即使实际类型包含toString3(),您也只能访问toString()和toString2(),而它类型为HawaiiNativeForestBirds。