• Post Reply Bookmark Topic Watch Topic
  • New Topic

Highest value in an array  RSS feed

 
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!  I have yet another question haha.  Arrays are still hard for me to grasp at the moment.


So, I'm trying to write a code that uses arrays inside methods. getTotal, getAverage, getHighest, and getLowest for test scores.

Everything was going well until I tried to create the gethighest method.   I'm getting this error:

91.0Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Grade.getHighest(Grade.java:44)
at Grade.main(Grade.java:16)


Here's the code so far:



I've tried fixing it with no luck, I don't even know why it thinks that "index" is an int, even when I declare it as a double.  Any help will be much appreciated!  Thanks!



 
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jacob,

well, looking at the 'getHighest' method, there are some errors in it.

line 1: you demand that 'index' < grades[0]'. Do you think that is correct? What, for example, if the array 'grades' only has 3 elements, but grades[0] = 10? Or when grades[0] = -4.12345?
line 2: you are printing out 'grades'. Shouldn't that be some other variable? And should that line be in the for loop?

Well, enough to think about for now!
 
Ranch Hand
Posts: 127
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try with this.

complete solution was removed by moderator

you have to use index < grades.length instead of index< highest.because we have to go through the array elements.
And other mistake is final result must return outside the for loop.
 
Piet Souris
Master Rancher
Posts: 2045
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jacob

any luck yet? Did you get it up and running?
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Sousie wrote:I've tried fixing it with no luck, I don't even know why it thinks that "index" is an int, even when I declare it as a double.

First off, good job on the code formatting. Keep it up because it makes your code easier to read.

On line 40, you declare highest as a double.
On line 41, in the for-loop header, you declare index as an int.

Your loop termination condition is incorrect, as Piet already pointed out. The loop terminates when you have gone through all the elements of the array. In other words, the loop index will go from 0 to something after the loop has traversed the full length of the array. What is that something? (Hint: I gave you a pretty obvious hint there just now)

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on Piet's reply,

on line 44, you print out a message that supposedly announces the highest grade you've found. And then you print out the grades array.  That's like taking a bag of balls and saying, "Hey, I found the biggest ball in this bag and here it is!" and then you hold out the entire bag. Does that make sense? This is essentially what you're doing on line 44.

Now, imagine yourself going through that bag of balls and saying, "Hey, I found the biggest ball in this bag and here it is!" and then you hold out the entire bag every time you find a ball that's bigger than the last biggest ball you've found so far. You do this over and over again until you've looked at all the balls in the bag. Again, does this make sense? Well, that's essentially what you're doing by putting line 44 inside the for-loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!