Reputation: 23
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();
}
}
}
}
Upvotes: 0
Views: 2664
Reputation: 1277
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.
Upvotes: 1
Reputation: 9650
Your main
method doesn't do anything: it just contains declarations.
Upvotes: 2
Reputation: 997
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();
}
}
Upvotes: -1