#logic problem with if statement

42 messages · Page 1 of 1 (latest)

hardy fractal
#
else if(strings.get(i).charAt(p)=='}') {
      if(brackets.peek()=='{') {
            brackets.pop();
      }else {                                                                   correctOrIncorrect.add(0);
        break;
}```
im having some trouble understanding the logic here. While debugging iv noticed that when the `if` condition is false, the `else` condition does not get executed...may someone please explain why this is, or am i just understanding it in a wrong way?
wide sonnetBOT
#

This post has been reserved for your question.

Hey @hardy fractal! Please use /close or the Close Post button above when you're finished. 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.

hardy fractal
#

sorry, idk why the code looks messed up🥲 😅

barren plaza
#

which if?

#

also, are you sure it isn't getting executed? correctOrIncorrect.add(0); seems like a nop, i'd expect a correctOrIncorrect.add(1) there, without knowing what correctOrIncorrect is

hardy fractal
#

when i try tp past the code it goes over the character limit im allowed...but yes, im sure its not getting executed because 0 is not being added. i understand its harder to find the error without looking at the whole thing, but i thought it would just be a logical issue involving the if statement, because according to what i know, if the if condition is false, the else condition should be executed, right?

barren plaza
#

yes

#

it might not be getting reached due to the else if above it being false, though.

#

which is why i asked

#

have you tried debugging

hardy fractal
#

let me try pasting it again. Yes i have tried to debug it, thats how i saw its not being executed🥺

#

i hope it comes out looking right...

#

it says this😢

barren plaza
#

try a bin

barren plaza
hardy fractal
barren plaza
#

these

hardy fractal
barren plaza
#

please don't use files

barren plaza
hardy fractal
#

okay cool👍

#

iv never used it before, let m try to see how it works👍

hardy fractal
barren plaza
#

uh, send it here?

hardy fractal
#

lol okay, let me just see how to send it...give me a sec😅

barren plaza
#

just copy the link

hardy fractal
#

im sorry it looks so convoluted🥺 🤦

barren plaza
#

so what makes you think it isn't getting executed?

#

also doesn't look like you're resetting brackets after each loop

hardy fractal
#

i think brackets is being cleared on lines 12-14?

barren plaza
#

oh my bad, completely missed that; something like that should be at the end of the loop, or just have brackets local to the loop. relying on the next iteration to handle a dirty state is error-prone.

hardy fractal
barren plaza
#

i feel like im missing a lot of context here on what specifically is the issue, the descriptions so far have been somewhat hard to interpret so im not sure im understanding correctly

#

are you saying neither the if nor the else are being executed?

hardy fractal
#

im trying to make a program that checks if a string has the correct opening and closing brackets
Like ([)] would be wrong and (){[<>]} would be right

#

but in terms of those if statements im really not sure if i think i know exactly what the problem is...i may be just misunderstanding my own problem🥲

barren plaza
#

i see you have a system there for handling multiple strings, can you explain that part

hardy fractal
#

basically im taking the string and placing it into an arraylist, and then the for loop uses the elements in that arraylist sequentially
the strings can look like
(((a * x) + [b] * y) + c which is wrong and auf(zlo)men [gy<psy>] four{s} which is right