#How do I use try catch to catch an array out of bounds exception?
209 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @neat scroll! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
im just confused how to use those variables above into the try statment
maybe i should change the exception, but i honestly am not sure about all the exceptions
you really shouldn't need to do this, this exception is thrown when you are trying to access an element in array at the wrong index
you can just check the index's value if you want to run code when it's outside the range
okie i will attempt this!
but then again you method name is leading me to believe this is an xy problem
The XY problem is a communication problem encountered in help desk, technical support, software engineering, or customer service situations where the question is about an end user's attempted solution (Y) rather than the root problem itself (X).The XY problem obscures the real issues and may even introduce secondary problems that lead to miscomm...
like i didnt give enough info?
yea probably
well what are you actually trying to achieve
averageInRange(new int[]{1, 2, 3, 4, 5, 6, 7, 8}, 2, 5) should return 4.5 and
averageInRange(new int[]{1, 2, 3, 4, 5, 6, 7, 8}, 5, 2) should return 0.0.
cause its not in range
I think i did it!?
well if I input an index outside the indices of the array, what happens?
you only need one if statement
it's the location of that if statement that matters
there should be a more speicifc location, like inside the method itself
can you copy and paste the stacktrace?
the what now
the red message
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 9 out of bounds for length 8
at assign04.MethodPractice.averageInRange(MethodPractice.java:52)
at assign04.MethodPractice.main(MethodPractice.java:80)
oh
80 and 52?
alright, look at this line assign04.MethodPractice.averageInRange(MethodPractice.java:52)
line 52 yeah
it's throwing there because your doing the following arr[some index less than 0 or greater than/equal to the length]
arr is some array
so you want to check the index before this happens
an if statement at the beginning
yes
bruh
it was that simple 😭
so is writing 2 if statements like this fine
or does this look like bad writing
it's incorrect and ugly :/
😭
what happens if either is -5 for instance
try it
with the failed expression?
sure
yeah that's no good, you need to be explicit. value > is not a boolean expression
well what are we trryna fix first tho
how do i make it like proper format
the "clean" way i guess?
my code is ugly 
indexStart < 0 is a boolean expression
so you could try implementing that into your code
you want to check if the indexStart is less than 0
idk what that is lol
stop guessing!
no i mean it WORKS
no it doesn't
yeah but again -5 is less than arraylength
wait, bad example
now if fails when the it's actually a valid index
any index less than the length now returns 0
you should check if either index is less than 0 or greater/equal to the length
you're almost there
HUHH
array[0] is accessing the element in the array
that has nothing to do with what you are checking
oh
u meant like int 0
yes
if you still have the check for the startIndex being greater than the endIndex, then sure
yea at the bottom
tysm karter i would have def forgotten to like check all that
negatives and out of bounds
etc
and i learned how to finally use the || or statement now
now i gotta watch some of my cs lectures and hope i dont fall behind
the logic itself seems flawed but maybe thats a problem for another time? lol
you're just getting the average of the range right?
yea
so why are you adding the index to each element lol
o
I guess that's a 1 not an i
your loop
what is it doing exactly
because this was also just a guess
but it printed out the right numbers
so
i was like ill take it
if you want to get you code right, you need to stop guessing 😭
yes because your average is calculated incorrectly
you aren't averaging the indices, you're averaging the elements in the range
,ask eval (3+4+5+6)/4
(3,4,5,6) are your elements in your range (if the range was [2,5])
4 is the number of elements
wait so array[i]+=1
am i making 3,4,5,6 into 4,5,6,7?
or am i makin it go to the next element in the array and adding that
like 3+4+5+6
yes
you shouldn't need to change the array
you are just adding the elements up and dividing by the length of the range
should or shouldnt idk if im understanding this right
shouldn't mb
okok yeah the second sentence made it sound like that lmao
i think thats what i thought it was doing
but
yea thats def not whats going on
wait idk whats the problem
when does it not work?
where are you doing this operation in your code
the average you are calculating is not the average of your indices, it's the average of your elements in the range
holdup lemme use a calculator my brain is dying
wait then why is it printing the write answers if the math is wrong
Did you try it with other input?
if i divide it by length right?
Most likely because your array sorted
i get 1.125
Yeah nvm, if you're dividing by the length it should be wrong. You might be getting lucky with your input
Do you understand the difference between an element and its index?
You are finding the average of the indices here
Yeah but you want the average of the elements in the array starting at indexStart to endIndex
but im just finding range?
isnt that just x + y /2
wait idek what im finding anymore
range is the middle number between
The range is the difference between the end and the start of a set of elements
Median would be the middle number
well shit idk
wtf am i calculating then
ill ask the teacher but right now its 1am and i gotta get up early for school so thank u a bunch for your help, ill ask again if im still stuck on this tmrw
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.