• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

ArrayIndexOutOfBoundsException

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79931
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79931
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79931
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79931
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79931
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I have just been at this game a lot longer than you. And …


“You're welcome”
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic