Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBoundsException--- Please help

 
kunagu varun
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

but

first element is arr[0]
second element is arr[1]
...
tenth element is arr[9]
 
kunagu varun
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
 
Ross Miller
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic