my dog learned polymorphism
The moose likes Beginning Java and the fly likes ArrayIndexOutOfBoundsException--- Please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayIndexOutOfBoundsException--- Please help" Watch "ArrayIndexOutOfBoundsException--- Please help" New topic

ArrayIndexOutOfBoundsException--- Please help

kunagu varun
Ranch Hand

Joined: Apr 13, 2010
Posts: 38
Hi the following code compiles and runs successfully but i get the error when i read tenth element.

The error:

What may be the problem
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
arr dimension is 10

first element is arr[0], the last is arr[9]

when you arrive at line 13 of your code you surely get that exception when i=10

substitute the for loop at line 10 with the following

Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Sorry, to complete my answer:

you should also substitute the same for loop at line 16 with the same i posted before

Remember arr.length = 10 (10 elements in your array)


first element is arr[0]
second element is arr[1]
tenth element is arr[9]
kunagu varun
Ranch Hand

Joined: Apr 13, 2010
Posts: 38
Thank you very much for the response "Nicola Garofalo"

What i actually need is to print the statement
Enter the value of element 1:......Enter the value of element 10: to get the ten elements

But in c language we used to give the same statement

in order to print Element 1 ..... Element 10

Why does it not work in java???
pete stein

Joined: Feb 23, 2007
Posts: 1561
kunagu varun wrote:
Why does it not work in java???

1) Java is not C
2) Regardless, I always thought that arrays were zero-based in C as well. Shows how much I know.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Pete is correct, that's totally wrong in C, too. The difference is that in C, nothing automatically lets you know that you've stomped on the data past the end of your array. So it might just work OK, or you might crash (the larger the program, the more likely this will eventually cause a crash.) Java reports the problem without letting it happen -- Java is much "safer" than C in that way.

In any case, in C, Java, C++, or any similar language, the elements of a ten-item array are numbered 0 through 9, not 1 through 10. If you want to print "enter element 1" for the first element, then use

System.out.println("Enter element " + (i+1));

[Jess in Action][AskingGoodQuestions]
Ross Miller

Joined: Jan 10, 2010
Posts: 7
You cannot access arr[11].

As our ranch hands explained in previous posts. In computer science you start counting things with zero as offset ! not 1.
Colin Wright

Joined: Apr 21, 2010
Posts: 8
Actually this is a very good example of the need for such an out of bounds error message, sounds like Kunagu has been writting bugged C code for some time and not realised it. May have led to some interesting logic errors.
kunagu varun
Ranch Hand

Joined: Apr 13, 2010
Posts: 38
ok guys... Thank you for your valuable responses....

When there are lot of advantages compared with other languages we have to adjust with java.. I will use the following statement

to start printing element 1

Thank you once again.....
I agree. Here's the link:
subject: ArrayIndexOutOfBoundsException--- Please help
It's not a secret anymore!