Forums Register Login

Highest value in an array

+Pie Number of slices to send: Send
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!



+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
@Jacob

any luck yet? Did you get it up and running?
+Pie Number of slices to send: Send
 

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)

1
+Pie Number of slices to send: Send
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.
I love a woman who dresses in stainless steel ... and carries tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 479 times.
Similar Threads
Array with occurrences
How to fix bad operand types for binary operator?
Does static modifier required ???
Inheritance question
ArrayIndexOutOfBoundsException:4
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:03:47.