Hey everyone,

I'm wondering why I can't print the values that are in the array. I'm passing an array to the method below and trying to print the result.. the compiler says that "ArrayIndexOutOfBoundsException"

What's the problem really?

public static int[] treble(int[] nums){

		for (int i=0; i <= nums.length; i++){
			nums[i] = 3*nums[i];
		}
		return nums;
		
}

in the main method, I have   int[] array = {1,2,4};
                              system.out.println(treble(array));

i <= nums.length is the problem

array is length 3, so valid indexes are 0,1,2, Your code executes with indexes 0,1,2,3

How is that causing the problem?

Your loop tries to access these array elements:
nums[0], nums[1], nums[2], nums[3]
but there is no nums[3]

In my opinion, that shouldn't be a problem because I haven't specified values for indexes 2 and 3. However, I changed the number of elements in my array to two elements and I'm still getting the same error. Anyway, thanks for your comment.

I got my method printing out the values that I pass to it, and after being multiplied by three. I'm, however, still getting the same error as before. I still don't know why.


Here is the code

public static int treble(int[] nums){
	for (int i=0; i <= nums.length; i++){
		nums[i] = 3*nums[i];
		println("array [" + i + "] = " + nums[i] );
	}
	return 1;
		
}
public static void main (String [] args){
	int[] array = {2,4,6, 10, 20};
	treble(array); 
}

Cheers,

In my opinion, that shouldn't be a problem because I haven't specified values for indexes 2 and 3. However, I changed the number of elements in my array to two elements and I'm still getting the same error. Anyway, thanks for your comment.

Unfortunately for you, in Java's opinion any attempt to reference an element bigger than the array is an error. If you have an array of 3 elements you can only refer to nums[0], nums[1], nums[2]. Any kind of reference to nums[3] is an error, whether you've specified values or not.

You still have exactly the same problem in your for loop. The way you coded it gives values for i of 0,1,2, and 3. When you use i=3 as an index for your array that's the error. Look again at your for loop, and especially it's termination condition.

commented: well explained for those who actually read it. +12

The equal sign in my condition was causing the problem. But, in plain English, we say that since nums is less or equal to the size of our array, treble nums by 3. That's why I initially thought that the condition shouldn't be problematic.

Thanks for the explanation.

Thanks for the brief explanation..

Was that sarcasm? I wanted you to work it out for yourself. That way you will really understand it, and never make that mistake or any variant of it again. I could have just posted a corrected version of line 3 from your original post, and you could have pasted that into your code - problem solved, nothing learned. It's the DaniWeb way.
J

Member Avatar for hfx642

JamesCherrill explained it in very simple language 3 times.
Don't fault HIM because YOU didn't understand.
The only way he could have given you a better answer is to give you the code.
We don't do that here!

I didn't want HIM to give me the code. I wanted to know where the problem in the loop was and more importantly, I wanted to learn. In fact, I was a bit surprised that the equal sign could cause the problem.

Hi rotten69. I'd appreciate some more detailed feedback here, because it's really difficult to get the balance between guiding/teaching someone and spoon-feeding them a quick solution. And sure, yes, I get it wrong sometimes.
Now you know the what and the why of this problem, perhaps you could post a little example of how it could have been best explained to someone in your position?
Thanks
James
ps: in case there's any doubt, this is not any kind of p*ss-take or sarcasm, it's a genuine request for feedback.

try this...this syntax might not be the answers that your expecting....

public class qwe
{
    public static void main(String []args)
    {
        int array []= {1,2,4};

        for( int x = 0; x < array.length; x++)
        {
            System.out.println(array[x]);
        }
    }
}

try this...this syntax might not be the answers that your expecting....

public class qwe
{
    public static void main(String []args)
    {
        int array []= {1,2,4};

        for( int x = 0; x < array.length; x++)
        {
            System.out.println(array[x]);
        }
    }
}

end quote.

This was exactly the issue, NewbieGuy, but JamesCherrill already explained this three times, the OP has been able to figure out what the problem was, has corrected his code and marked this thread as solved.
so .... why still post this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.