• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBoundsException  RSS feed

 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found this assignment where i am supposed to get scores input then grade the students according to their scores but i am getting this exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at testing.Arrays.main(Arrays.java:23)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fady zohdy wrote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at testing.Arrays.main(Arrays.java:23)


Can you show us which is line 23 of your program? We don't know if you show us everything -- actually, you didn't since we know your class is in a package, so, we don't know which is line 23.

Henry
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

Can you show us which is line 23 of your program? We don't know if you show us everything -- actually, you didn't since we know your class is in a package, so, we don't know which is line 23.

Henry


i am really sorry . i modified in the code before submitting it here. here is the right exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at testing.Arrays.main(Arrays.java:19)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fady zohdy wrote:
i am really sorry . i modified in the code before submitting it here. here is the right exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at testing.Arrays.main(Arrays.java:19)


Well, assuming that this line is close, it would be caused by this line...

which can be caused by two possibilities. However, from the code, we can eliminate one.

It is most likely that your input doesn't split to four components -- in fact, from the error message, it only splits to one element.

Henry
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Well, assuming that this line is close, it would be caused by this line...

which can be caused by two possibilities. However, from the code, we can eliminate one.

It is most likely that your input doesn't split to four components -- in fact, from the error message, it only splits to one element.

Henry

you are right. this line is the cause and the input does split to one element(the first one).
this is my input separated by blank spaces: 40 80 90 50
how do i split it to four components?
and thanks for the help
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fady zohdy wrote:
you are right. this line is the cause and the input does split to one element(the first one).
this is my input separated by blank spaces: 40 80 90 50
how do i split it to four components?
and thanks for the help


Hint: Check the scanner input. It may not be the split() that is the issue.

Henry
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a misunderstanding of how Scanners work. What you are doing is reading something from the keyboard with next(). That reads a single “token” separated from its successor by “whitespace”. So if you write
40 80 90 50
it reads "40", i.e. a String with a 4 character and a 0 character. Not a number. If you try to split that String with spaces in the split method it splits into a one‑element array. So you can use element 0 but not element 1.

More information to follow in a few minutes.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a method called nextLine which you can pass "40 80 90 50" to, but for reasons explained in this post, you will only get that input the second time you call nextLine after nextInt.

But why are you reading a line, splitting it, and then trying to parse each token. There is a simpler way to do it. To follow in a few minutes.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You definitely want 4 marks per student? Why not simply set up a 4‑element array and iterate it with a for loop (not for‑each). Then you can initialise each of the 4 elements with Scanner#nextInt.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are entering 4 values from the keyboard you need a 4‑element array to hold those 4 values. You are setting the length of the array to a different amount, to be requested from the keyboard. I shall leave you to decide the best way to sort that problem out.
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you are entering 4 values from the keyboard you need a 4‑element array to hold those 4 values. You are setting the length of the array to a different amount, to be requested from the keyboard. I shall leave you to decide the best way to sort that problem out.


thanks man. you are awesome.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I have just been at this game a lot longer than you. And …


“You're welcome”
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!