0

I made a method that returns a string. I'm trying to allow the string to be expanded through a for loop like so:

public static String averageFood(){
    String average = "";
    String temp = "";
    for (int i = 0; i < n1; i++){
        temp = myGerbils[i].name + " (" + (myGerbils[i].getLabID())+ ") " + (percentFood(i)) + "%" + "\n";
        average = temp + average;
    }
    return average;
}

It calls upon another method in the string. When I run it, it goes fine up until it gets to the percentFood method; then, I get an error that looks like "[I@6158dd66". The percentFood Method looks like this:

public static int[] percentFood(int i){
    int temp;
    int temp2;
    int[] ans = new int[n1];
    for (int j = 0; j < n1; j++){
        temp = myGerbils[i].getFoodCon(j);
        temp2 = gerbilFood[j].getMaxAm();
        ans[j] = (temp/temp2);
    }
    return ans;
}

Any ideas? Thanks!

2

2 Answers 2

1
[I@6158dd66  

This is not error you are printing object reference not it's value.

 (percentFood(i)) 

this will return array ans and you are concating it directly if you want to print Array you can do like this.

Arrays.toString(ans);

Or if you want to create String or Total by array element you can do it like this.

int arr[]=(percentFood(i));

for(int j:arr)
{
//concat or total
}
Sign up to request clarification or add additional context in comments.

Comments

0

percentFood returns an entire array, but in your code, you are calling percentFood(i) - this will return an entire array, and as an array is an object, that error you are seeing is actually the space in memory that the object is stored.

You would need to grab the array first, then iterate through it.

Comments

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.