Java - 使用Scanner构建对象数组

时间:2018-01-05 11:22:41

标签: java arrays java.util.scanner

我正在尝试使用用户的输入构建一个对象数组。我的代码运行没有错误但是输出窗格是空白的并且说“构建成功”我无法弄清楚我做错了什么因为我确信所有代码都是正确的。任何指针都将非常受欢迎。提前谢谢

package sanctuary;

import java.util.Scanner;

/**
 *
 * @author dell
 */
public class Sanctuary {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

class Animal
    {
    private String species;
    private String animalname;
    private String breed;
    private double weight;
    private String gender;
    private int age;
    private String location;
    private String vet;
    private double vaccine;
    private double medicine;
    private double food;
    private double muandv;

        void GetAnimalData()           // Defining GetAnimalData()
        {

            Scanner sc = new Scanner(System.in);

            System.out.print("\n\tEnter Animal Species : ");
            species = (sc.nextLine());

            System.out.print("\n\tEnter Animal Name : ");
            animalname = sc.nextLine();

            System.out.print("\n\tEnter Animal Breed : ");
            breed = (sc.nextLine());

            System.out.print("\n\tEnter Animal Weight : ");
            weight = (sc.nextDouble());

            System.out.print("\n\tEnter Animal Gender : ");
            gender = (sc.nextLine());

            System.out.print("\n\tEnter Animal Age : ");
            age = Integer.parseInt(sc.nextLine());

            System.out.print("\n\tEnter Animal Location : ");
            location = (sc.nextLine());

            System.out.print("\n\tEnter Vet Name: ");
            vet = (sc.nextLine());

            System.out.print("\n\tEnter Vaccine Cost : ");
            vaccine = (sc.nextDouble());

            System.out.print("\n\tEnter Medicine Cost : ");
            medicine = sc.nextDouble();

            System.out.print("\n\tEnter Food Cost : ");
            food = (sc.nextDouble());

            System.out.print("\n\tEnter Maintenance, Utility and Vet Cost : ");
            muandv = (sc.nextDouble());

        }

        void PrintAnimalData()           // Defining PrintAnimalData()
        {
            System.out.print("\n\t" + species + "\t" +animalname + "\t" +breed + "\t" +weight + "\t" +gender + "\t" +age + "\t" +location + "\t" +vet + "\t" +vaccine + "\t" +medicine + "\t" +food + "\t" +muandv);
        }

        public void main(String args[])
        {

            Animal[] AnimalList = new Animal[100];
            int i = 0;

            for(i=0;i<AnimalList.length;i++)
                AnimalList[i] =  new Animal();   // Allocating memory to each object

            for(i=0;i<AnimalList.length;i++)
            {
                System.out.print("\nEnter details of "+ (i+1) +" Animal\n");
                AnimalList[i].GetAnimalData();
            }

            System.out.print("\nAnimal Details\n");
            for(i=0;i<AnimalList.length;i++)
                AnimalList[i].PrintAnimalData();

        }
}
    }
}

3 个答案:

答案 0 :(得分:2)

您的main方法无效:它只包含声明。

答案 1 :(得分:1)

第一种情况是创建一个参数化构造函数来访问私有数据 - 用于编写

package stackoverflow.foo.sanctuary;

public class AnimalParametrizedConstructor {
    private String name;
    private String desc;


    public AnimalParametrizedConstructor(String name, String desc) {
        super();
        this.name = name;
        this.desc = desc;
    }

    //when no getters, that is the only way how to access to class fields for print outside
    @Override
    public String toString() {
        return "name: " + this.name + ", description: " + this.desc;
    }
}

第二种方法是为特定字段正确生成getter和setter

package stackoverflow.foo.sanctuary;

public class AnimalGettersSetters {
    private String name;
    private String desc;

    AnimalGettersSetters(){
        //this is implicit constructor in java, you dont need to define it
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }   
}

然后,您可以在任何地方从类外部调用打印方法(方法必须公开),或使用getter获取其值。 对于写入,您可以在第一种情况下使用参数化构造,或在第二种情况下使用setter。

请注意,当你想要填充数组时,你必须在两种情况下创建新的对象实例 - AnimalParametrizedConstructor[] paramAnimals = new AnimalParametrizedConstructor[COUNT];只是一个声明,而不是创建实例(否则你将获得nullpointer exception

package stackoverflow.foo.sanctuary;

import java.util.Scanner;

public class SanctuaryMainClassWhateverName {

    public static void main(String[] args) {
        final int COUNT = 2;

        Scanner sc = new Scanner(System.in);

        // parametrized constructor section
        // load animals with parametrized constructor
        AnimalParametrizedConstructor[] paramAnimals = new AnimalParametrizedConstructor[COUNT];

        for (int i = 0; i < COUNT; i++) {
            System.out.println("What a name?");
            String name = sc.nextLine();
            System.out.println("What a description?");
            String desc = sc.nextLine();

            AnimalParametrizedConstructor newAnimal = new AnimalParametrizedConstructor(name, desc);
            paramAnimals[i] = newAnimal;
        }

        // and print them- because we defined toString, we have access to fields without
        // getter
        for (int i = 0; i < paramAnimals.length; i++) {
            System.out.println("animal no. " + i + ": " + paramAnimals[i].toString());
        }

        // animals with getter and setter section
        AnimalGettersSetters[] animalsGS = new AnimalGettersSetters[COUNT];

        for (int i = 0; i < COUNT; i++) {
            AnimalGettersSetters newGS = new AnimalGettersSetters();
            // load
            System.out.println("What a name?");
            newGS.setName(sc.nextLine()); // we have setters to private fields!
            System.out.println("What a description?");
            newGS.setDesc(sc.nextLine()); // we have setters to private fields!

            animalsGS[i] = newGS;
        }

        // print using gettes
        for (int i = 0; i < COUNT; i++) {
            System.out.println(
                    "animal no." + i + ": name: " + animalsGS[i].getName() + ", desc: " + animalsGS[i].getDesc());
        }

        // NEVER FORGET !
        sc.close();

    }
}

老实说,你应该寻找一些OOP教程来开始。

答案 2 :(得分:-1)

您的错误是您的节点在public static void main(String args [])中添加了任何可执行代码,因此程序不显示任何输出。

   import java.util.Scanner;

public class Sanctuary {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

class Animal
    {
    private String species;
    private String animalname;
    private String breed;
    private double weight;
    private String gender;
    private int age;
    private String location;
    private String vet;
    private double vaccine;
    private double medicine;
    private double food;
    private double muandv;

        void GetAnimalData()           // Defining GetAnimalData()
        {

            Scanner sc = new Scanner(System.in);

            System.out.print("\n\tEnter Animal Species : ");
            species = (sc.nextLine());

            System.out.print("\n\tEnter Animal Name : ");
            animalname = sc.nextLine();

            System.out.print("\n\tEnter Animal Breed : ");
            breed = (sc.nextLine());

            System.out.print("\n\tEnter Animal Weight : ");
            weight = (sc.nextDouble());

            System.out.print("\n\tEnter Animal Gender : ");
            gender = (sc.nextLine());

            System.out.print("\n\tEnter Animal Age : ");
            age = Integer.parseInt(sc.nextLine());

            System.out.print("\n\tEnter Animal Location : ");
            location = (sc.nextLine());

            System.out.print("\n\tEnter Vet Name: ");
            vet = (sc.nextLine());

            System.out.print("\n\tEnter Vaccine Cost : ");
            vaccine = (sc.nextDouble());

            System.out.print("\n\tEnter Medicine Cost : ");
            medicine = sc.nextDouble();

            System.out.print("\n\tEnter Food Cost : ");
            food = (sc.nextDouble());

            System.out.print("\n\tEnter Maintenance, Utility and Vet Cost : ");
            muandv = (sc.nextDouble());

        }

        void PrintAnimalData()           // Defining PrintAnimalData()
        {
            System.out.print("\n\t" + species + "\t" +animalname + "\t" +breed + "\t" +weight + "\t" +gender + "\t" +age + "\t" +location + "\t" +vet + "\t" +vaccine + "\t" +medicine + "\t" +food + "\t" +muandv);
        }

        public void main(String args[])
        {



        }
}

Animal[] AnimalList = new Animal[100];
int i = 0;

for(i=0;i<AnimalList.length;i++)
    AnimalList[i] =  new Animal();   // Allocating memory to each object

for(i=0;i<AnimalList.length;i++)
{
    System.out.print("\nEnter details of "+ (i+1) +" Animal\n");
    AnimalList[i].GetAnimalData();
}

System.out.print("\nAnimal Details\n");
for(i=0;i<AnimalList.length;i++)
    AnimalList[i].PrintAnimalData();
    }
}