Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm attempting to save an "x" amount of integers inside of an object class. I'm trying it via an array but am not sure if this is possible and as of now eclipse is giving me two errors. One asking me to insert an Assignment operator inside of my Gerbil() class and another saying that I can't make a static reference to to the non-static field "food." The result I'm looking for is food 1 = first input; food 2 = second input; until it hits the total amount of food.

Here's my code so far:

import java.util.Scanner;
public class Gerbil {

public String name;
public String id;
public String bite;
public String escape;
public int[] food;

public Gerbil() {
  this.name = "";
  this.id = "";
  this.bite = "";
  this.escape = "";
  this.food[]; // I'm not sure what I should put here. This is where I want to store
}              // the different integers I get from the for loop based on the
               // total number of foods entered. So if totalFoods is 3, there should
               // be 3 integers saved inside of the object class based on what's typed
               // inside of the for-loop. Or if totalFoods = 5, then 5 integers.

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("How many foods?");
int totalFood = keyboard.nextInt();

System.out.println("How many gerbils in the lab?");

int numberOfGerbils = keyboard.nextInt();
Gerbil[] GerbilArray = new Gerbil[numberOfGerbils];

for(int i = 0; i <= numberOfGerbils; i++){
    GerbilArray[i] = new Gerbil();

    System.out.print("Lab ID:");
    String id = keyboard.next();

    System.out.print("Gerbil Nickname:");
    String name = keyboard.next();

    System.out.print("Bite?");
    String bite = keyboard.next();

    System.out.print("Escapes?");
    String city = keyboard.nextLine();

    for (int j = 0; j < totalFood; j++) {
        System.out.println("How many of food " + (j+1) + "do you eat?:");
        food[j] = keyboard.nextInt();
    }

}
}
}
share|improve this question
    
food[j] = keyboard.nextInt();. Whose food is this? –  Karolis Juodelė Apr 19 at 4:00
    
food[j] is gerbilArray[i]'s food. I want it to run for j = total number of food. But I want it to do be able to save each specific food, and do this for each gerbil. So gerbilArray[i+1] will be asked the same, and can save different numbers for each food food[j]. –  newJavaUser Apr 19 at 4:12
    
It's because of the i <= numberOfGerbils. It should just be i < numberOfGerbils –  Simon Corcos Apr 19 at 4:27
    
Just because you found an answer, does not mean you should clear out the question and tell us that it must be deleted. –  Takendarkk Apr 20 at 23:09
add comment

1 Answer

up vote 1 down vote accepted

You need to pass the number of food in the Gerbil constructor :

public Gerbil(int totalFood) {
   this.name = "";
   this.id = "";
   this.bite = "";
   this.escape = "";
   this.food[] = new int[totalFood]; 
}

And then in the loop will look like this :

for(int i = 0; i <= numberOfGerbils; i++){
GerbilArray[i] = new Gerbil(totalOfFood);

System.out.print("Lab ID:");
String id = keyboard.next();

System.out.print("Gerbil Nickname:");
String name = keyboard.next();

System.out.print("Bite?");
String bite = keyboard.next();

System.out.print("Escapes?");
String city = keyboard.nextLine();

for (int j = 0; j < totalFood; j++) {
    System.out.println("How many of food " + (j+1) + "do you eat?:");
    GerbilArray[i].food[j] = keyboard.nextInt();
}

}

Or something like that should do it.

share|improve this answer
    
Correction: This worked! Thank you! –  newJavaUser Apr 19 at 4:21
    
Then you should vote for my answer. This is the whole concept of this website. –  Simon Corcos Apr 19 at 4:37
    
I am, and do vote. But it puts a time limit on when I can. –  newJavaUser Apr 19 at 4:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.