0

I'm attempting to build an Array of Objects using input from the user. My code is running without error but the output pane is blank and says 'Build Successful' I can't work out what I have done wrong because I am sure all the code is correct. Any pointers would be most appreciated. Thank you in advance

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
  • If I have the class separately, how do I get the main method to use that class? Commented Jan 5, 2018 at 11:28
  • 1
    you need to move the code out from public void main(String args[]) in to the actual public static void main method. Please check the snippet I have added in the answers section Commented Jan 5, 2018 at 11:30
  • Man, you completly broke the OOP and encapsulation standards... The problem with access to the class fields when it was in another file is because private modifier, you can can change it, or better way- make parametrized construtor for animals or setters for these params.. Kindly, please look eg. over there ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html Commented Jan 5, 2018 at 11:47

3 Answers 3

2

Your main method doesn't do anything: it just contains declarations.

Sign up to request clarification or add additional context in comments.

3 Comments

exactly, thats what I mentioned in my answer below. I do not know why others fail to understand that.
@akshayapandey OK, I +1ed Your answer
thanks mate!!..Upvotes and downvotes do not bother me..its basically about some people who spread incorrect information to newbies and others.
1

First case is to make an parametrized constructor for access to the private data- for writing

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

Second way is to properly generate getters and setter for specific fields

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

Then you can call printing method from outside of the class anywhere you want (method must to be public), or to use getters for obtaining their values. For writing you can use parametrized constuctor in first case, or setters in second case.

Please, notice that you have to create new object instance in both of cases when you want to fill the array- AnimalParametrizedConstructor[] paramAnimals = new AnimalParametrizedConstructor[COUNT]; is just a declaration, not creating the instances (otherwise you will got 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();

    }
}

Honestly, you should to look for some OOP tutorials for beginning.

Comments

-1

Your mistake is that you have node added any executable code in your public static void main(String args[]), thus program does not show any output.

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

3 Comments

No mate, that is not cause, he is not working with params in the app, args are command-line params, but he takes the parameters from System.in
Please use the code I have posted, its is working absolutely fine. At least try and see the actual result before commenting.
any throughts on this. I do not know how people withlout knowing the stuff keep commenting. */ public static void main(String[] args) { class ABC{ int var=9; } ABC a=new ABC(); System.out.println(a.var); } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.