【问题标题】:Error: suitable constructor found and cannot find symbol错误:找到合适的构造函数但找不到符号
【发布时间】:2016-09-27 20:28:11
【问题描述】:

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

我正在使用一个名为birds.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

这是我的输入文件:

【问题讨论】:

  • 您没有定义接受您传递的参数的构造函数。查看您的构造函数,并确定您认为应该调用哪个构造函数
  • 那么我应该更改哪一个来解决所有这些问题?改用 HawaiiNativeForestBirdsWithMoreData?
  • 构造函数告诉你它期望什么参数,有多少个。例如HawaiiNativeForestBirds(String name, String scientificname, String color, Integer population)。如果您传入的不是namescientificNamecolorpopulation按此顺序),那么您的代码将不起作用。注意错误信息中的行号,看不懂的错误查找。
  • 设法解决了这个问题,但我只剩下 1 个错误。问题是我不能使用 tostring3 但是当我尝试 tostring2 它可以工作但不显示我的最后一列(作者)并且它是每一行的值

标签: java arrays inheritance subclass


【解决方案1】:

问题可能不在于您的构造函数,而在于您如何声明鸟的实例。你有,构造函数为(字符串,字符串,字符串,整数,字符串),但你的数据是按顺序(字符串,字符串,整数,字符串)。仔细检查您的 csv 文件中的顺序,并确保它与您传递参数的顺序相匹配。

编辑:检查 csv 文件后。人口是列表中的第 4 项,所以

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

另外,正如所指出的,传入了第 5 个参数,因此您需要更新构造函数以适应它。

编辑最后一个错误:

数组的数据类型并不需要使用 toString3() 方法。当它是 HawaiiNativeForestBirds 类型时,您只能访问 toString() 和 toString2(),即使实际类型包含 toString3()。

【讨论】:

  • 构造函数接受 4 个参数。您传递了 5 个参数,OP 也是如此。这是第一个问题。
  • 是的,我正在尝试传递 5 个参数
  • HawaiiNativeForestBirds 只有 toString() 和 toString2。使用 toString3() 需要 HawaiiNativeForestBirdsWithMoreData。您用于存储鸟类的数组具有 HawaiiNativeForestBirds 类型,因此即使实际对象具有该方法,它也无法访问 toString3()。
  • @Brion 由于您的评论解决了问题,您应该更新您的答案以包含该修复程序...:)
  • @Siegfraud245 方法toString3 在类HawaiiNativeForestBirds 上不存在。要使用它,您必须转换为子类类型:System.out.println(i + " " + ((HawaiiNativeForestBirdsWithMoreData) hbird).toString3());。或者,您可以更改数组的类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2022-01-11
  • 2016-03-24
相关资源
最近更新 更多