infile class。构造函数未定义

时间:2013-12-08 18:35:08

标签: java

它一直告诉我,我的构造函数Pet未定义。有什么想法吗?

我尝试在while循环之前在main方法中定义Pet,但它给了我同样的问题。

import java.util.*;
import java.io.*;

public class ReadPets
{
     public static void main (String[] args)
     {

          ArrayList <Pet> petList = new ArrayList <Pet>();
          Scanner inFile = null;
          String name;
          Pet p;
          try
          {
               inFile = new Scanner
               (new FileInputStream ("pets.txt"));
          }

          catch(FileNotFoundException e)
          {
               System.out.println("problem opening file.");
               System.exit(0);
          }
          while (inFile.hasNextLine())
          {
               name = inFile.nextLine();
               p = new Pet(name); // here is where my error is
               petList.add(p);
          }
          inFile.close();
     }
}

这是我的Pet课程。

public class Pet
{
     private String name;
     private int age; //in years
     private double weight; //in pounds

     /**
      This main is just a demonstration program.
     */
     public static void main(String[] args)
     {
         Pet myDog = new Pet( );
         myDog.set("Fido", 2, 5.5);
         myDog.writeOutput( );
         System.out.println("Changing name.");
         myDog.set("Rex");
         myDog.writeOutput( );
         System.out.println("Changing weight.");
         myDog.set(6.5);
         myDog.writeOutput( );
         System.out.println("Changing age.");
         myDog.set(3);
         myDog.writeOutput( );
     }

     public void writeOutput( )
     {
         System.out.println("Name: " + name);
         System.out.println("Age: " + age + " years");
         System.out.println("Weight: " + weight + " pounds");
     }

     public void set(String newName)
     {
         name = newName;
         //age and weight are unchanged.
     }

     public void set(int newAge)
     {
         if (newAge <= 0)
         {
             System.out.println("Error: illegal age.");
             System.exit(0);
         }
         else
             age = newAge;
         //name and weight are unchanged.
     }

     public void set(double newWeight)
     {
         if (newWeight <= 0)
         {
             System.out.println("Error: illegal weight.");
             System.exit(0);
         }
         else
             weight = newWeight;
         //name and age are unchanged.
     }

     public void set(String newName, int newAge, double newWeight)
     {
         name = newName;
         if ((newAge <= 0) || (newWeight <= 0))
         {
             System.out.println("Error: illegal age or weight.");
             System.exit(0);
         }
         else
         {
             age = newAge;
             weight = newWeight;
         }
     }

     public String getName( )
     {
         return name;
     }


     public int getAge( )
     {
         return age;
     }
     public double getWeight( )
     {
         return weight;
     }
}

3 个答案:

答案 0 :(得分:1)

你的Pet类没有一个构造函数,它使用String这就是你在尝试使用这一行

p = new Pet(name)

制作一个或做类似于演示的内容,即制作新的Pet实例,然后在此实例上调用set("Name")

像:

p = new Pet();
p.set(name);

答案 1 :(得分:0)

我在Pet类中没有看到任何构造函数,这意味着Java将为您提供默认(无参数)构造函数。

这就是以下陈述没问题的原因:

Pet myDog = new Pet( );

您遇到问题的声明使用带有参数的构造函数,但您尚未定义。

您需要创建此类构造函数(Pet(String name) {...})或更改逻辑以使用默认构造函数和set()方法。

一旦定义了任何构造函数,就会收到警告,Java将不再为您创建默认构造函数,因此您需要自己完成。

答案 2 :(得分:0)

您需要在Pet类中使用一个String编写参数化构造函数。