• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compliation error when using an array on learning website “Learneroo" (Java)  RSS feed

 
Alex Drummond
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Java challenge on this codecademy style learning website 'Learneroo' is:

"You're writing a program to to analyze test grades, and would like to know the sum of 3 "representative" grades that are stored in an array.
You will be given an array with an odd number of elements. Print the sum of the first, middle and last element in the array. For example, when given {1,2,3,4,5}, print 9 (1+3+5). Each sum should be printed on its own line."

My attempt embedded inside this code:



The error code I get is this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Main.doStuff(Main.java:5)
at Main.main(Main.java:22)

Any help would be much appreciated.

Josh


 
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

Hint: regarding basic math...

What is the answer to ... 100 + 1 / 2 ?


Henry

 
Alex Drummond
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The challenge states that I will be given an array with an odd number of elements. Therefore if I want to find the middle element in an odd numbered array surely x+1/2 always gets me there? I could be wrong.
 
Alex Drummond
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh wow, now I feel silly. I only just realized that the length is always 1 more than the last slot number so no need to adjust for odd numbers! Thank you!
 
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
Alex Drummond wrote:Therefore if I want to find the middle element in an odd numbered array surely x+1/2 always gets me there? I could be wrong.


Try it out on a calculator. Try typing it into Google.

Henry
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
note that 1 divided by 2 in integer arithmetic is always zero
 
Alex Drummond
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh dear, looks like I need to go back to primary school and learn BODMAS all over again, haha. Order of operations!! Thanks guys I've fixed it now.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a ten‑element array what is the middle element's index?
If you have an eleven‑element array what is the middle element's index?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!