#error
1 messages ยท Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
u have a logic error
lets approach it the same way as before
u have to learn how to debug these small issues urself ๐
the error says ur trying to access the sixth element (index 5) of a list that only has 5 elements
thats obviously wrong, hence crash
if u execute the code in ur IDE and not on the website, u will see whats going on
the full error message tells u that line 29 crashes
and it happens for an example where the number to search isnt in the list
so lets say ur list is [1, 2, 3]
and ur searching for 8
how to approach this? again, u would put prints in ur code to inspect whats going on
(or use ur debugger)
System.out.println("Looking at index " + i)
if u put that into ur while loop, line 28, u will see whats happening
it will tell u sth like:
Looking at index 0
Looking at index 1
Looking at index 2
Looking at index 3
Crash
for the example [1, 2, 3]
so. if u look at that, u should immediately notice the problem
Looking at index 3
this shouldnt be there
so. why is it there?
after all, u created ur abort-code if (i == list.size()) { break; }
well, inspect ur logic
this piece of logic is totally misplaced
ur code is separated into two blocks here
the first is "we found it!"
and the second is "we did not find it"
and u placed ur abort code into the "we found it!" part
why?
ur supposed to abort before reaching the end
the reason why u get the crash is bc ur executing list.get(i) on the index thats already out of the list
i.e. ur doing list.get(3)
and only after that, and only if that would be the number to look for, then u would have ur abort-code running
thats too late and misplaced as well
๐
Bro sorry if it's silly but i put it because if the same number is present in two different indicies then the code needs to identify both of those and print that they found both
And i need to inspect the course till the end
If I don't it will not identify the next index with the same number
Got my point?
then why would it break though?
that would end the loop
yes. but ur missing code that stops the iteration as soon as u hit the end of the list
u should step back
first write code that iterates the full list and prints each element
then wrap the print with an if so it only prints the elements that match
step by step
dont write everything in one attempt
otherwise u end up with multiple bugs