#How do I use try catch to catch an array out of bounds exception?

209 messages · Page 1 of 1 (latest)

neat scroll
strong elkBOT
#

This post has been reserved for your question.

Hey @neat scroll! Please use /close or the Close Post button 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.

neat scroll
#

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

compact salmon
# neat scroll

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

neat scroll
#

oh

#

no reason for try catch then?

compact salmon
#

yeah

#

just use and if statement

neat scroll
#

okie i will attempt this!

compact salmon
#

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...

neat scroll
#

yea probably

compact salmon
#

well what are you actually trying to achieve

neat scroll
#

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!?

compact salmon
#

well if I input an index outside the indices of the array, what happens?

neat scroll
#

ah shit i gotta try that

#

yea an exception

#

another if statement then?

compact salmon
#

you only need one if statement

#

it's the location of that if statement that matters

neat scroll
#

location?

#

i tried something like this

#

but didnt work still got an exception

compact salmon
#

look at your error

#

where is it orginating from in your code, what line

neat scroll
#

out of bounds

#

tried setting indexStart as 9

compact salmon
#

there should be a more speicifc location, like inside the method itself

neat scroll
#

maybe 63?

compact salmon
#

can you copy and paste the stacktrace?

neat scroll
#

the what now

compact salmon
#

the red message

neat scroll
#

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?

compact salmon
#

alright, look at this line assign04.MethodPractice.averageInRange(MethodPractice.java:52)

#

line 52 yeah

neat scroll
#

whats wrong with that?

#

it was working before

compact salmon
#

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

neat scroll
#

oh

#

so do i need to move it up

compact salmon
#

so you want to check the index before this happens

neat scroll
#

an if statement at the beginning

compact salmon
#

yes

neat scroll
#

bruh

#

it was that simple 😭

#

so is writing 2 if statements like this fine

#

or does this look like bad writing

compact salmon
#

it's incorrect and ugly :/

neat scroll
#

😭

compact salmon
#

what happens if either is -5 for instance

neat scroll
#

sigh

#

another failure

#

cant do smth like > || < like greater OR less than

compact salmon
#

try it

neat scroll
#

i did

#

invalid expression

compact salmon
#

oh I read that as can

#

can you show the code

neat scroll
#

with the failed expression?

compact salmon
#

sure

neat scroll
compact salmon
#

yeah that's no good, you need to be explicit. value > is not a boolean expression

neat scroll
#

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 hidethepain

compact salmon
#

indexStart < 0 is a boolean expression

#

so you could try implementing that into your code

neat scroll
#

huh

#

so is this uglier

#

LMAO

compact salmon
#

you want to check if the indexStart is less than 0

#

idk what that is lol

#

stop guessing!

neat scroll
#

no i mean it WORKS

compact salmon
#

no it doesn't

neat scroll
#

wym

#

it checks if its greater or less than arraylength

#

and now it returns 0.0

compact salmon
#

yeah but again -5 is less than arraylength

neat scroll
compact salmon
#

wait, bad example

#

now if fails when the it's actually a valid index

#

any index less than the length now returns 0

neat scroll
#

oh

#

fml

#

but i did check if index was less than 0 though?

compact salmon
#

no

#

you only checked if it was less than the length

neat scroll
#

o

#

ok holdup

compact salmon
#

you should check if either index is less than 0 or greater/equal to the length

neat scroll
#

oh

#

u mean like combien it into 2 lines instead of 4

compact salmon
#

you're almost there

neat scroll
#

HUHH

compact salmon
#

array[0] is accessing the element in the array

#

that has nothing to do with what you are checking

compact salmon
#

yes

neat scroll
#

ok i read that wrong and was like

#

array[0] lol

#

alright well i think its good now?

compact salmon
#

if you still have the check for the startIndex being greater than the endIndex, then sure

neat scroll
#

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

compact salmon
#

the logic itself seems flawed but maybe thats a problem for another time? lol

neat scroll
#

HU

#

wym 😭

#

my code?

compact salmon
#

you're just getting the average of the range right?

neat scroll
#

yea

compact salmon
#

so why are you adding the index to each element lol

neat scroll
#

o

compact salmon
#

I guess that's a 1 not an i

neat scroll
#

wait a min

#

does it only work because the array is 1,2,3,4,5,6,7

#

shit

#

NOOOOOO

compact salmon
#

it works if the index are messed up lol

#

because that's what we just worked on

neat scroll
#

are u talking about this

#

or

compact salmon
#

your loop

neat scroll
#

this

compact salmon
#

what is it doing exactly

neat scroll
#

because this was also just a guess

#

but it printed out the right numbers

#

so

#

i was like ill take it

compact salmon
#

if you want to get you code right, you need to stop guessing 😭

neat scroll
#

oh

#

was this loop completely pointless

compact salmon
#

no you need to calculate the average still

#

a loop can help you with that

neat scroll
#

well my loop did nothing tho

#

right

#

or nothing useful'

compact salmon
#

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

compact salmon
#

(3,4,5,6) are your elements in your range (if the range was [2,5])

#

4 is the number of elements

neat scroll
#

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

compact salmon
#

you shouldn't need to change the array

#

you are just adding the elements up and dividing by the length of the range

neat scroll
compact salmon
#

shouldn't mb

neat scroll
#

okok yeah the second sentence made it sound like that lmao

neat scroll
#

but

#

yea thats def not whats going on

#

wait idk whats the problem

#

when does it not work?

compact salmon
#

the average you are calculating is not the average of your indices, it's the average of your elements in the range

neat scroll
#

holdup lemme use a calculator my brain is dying

#

wait then why is it printing the write answers if the math is wrong

compact salmon
#

Did you try it with other input?

neat scroll
#

if i divide it by length right?

compact salmon
#

Most likely because your array sorted

neat scroll
#

i get 1.125

compact salmon
#

Yeah nvm, if you're dividing by the length it should be wrong. You might be getting lucky with your input

neat scroll
#

idk wtf is going on anymore

#

lmao

compact salmon
#

Do you understand the difference between an element and its index?

compact salmon
neat scroll
#

element is the literal number?

#

and index is the location of the array

compact salmon
#

Yeah but you want the average of the elements in the array starting at indexStart to endIndex

neat scroll
#

isnt that just x + y /2

#

wait idek what im finding anymore

#

range is the middle number between

compact salmon
#

The range is the difference between the end and the start of a set of elements

#

Median would be the middle number

neat scroll
#

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

strong elkBOT