Convert boolean inside if statement

Hello, I am working on a program where I am using a particular logic for a flag, I am declaring a boolean variable, and then making an if statement that executes if the statement and then add value to an array, but I want to alternatively add values to my array. EG I have 1001 I want to add 1 in array1, 0 in both arrays, 0 in both arrays, 1 in array1. This is how I have written this in code. But the problem is that the boolean is always true, it doesn't get flase even after the if statement has executed. This is my code:
for(int i = 0; i < reversedBinaryValue.size(); i++)
{
boolean addToSplitOne = true;

// since arrays are already filled with zeroes, it skips an additional step of
// filling with zeros
if(reversedBinaryValue.get(i) == 1)
{
// now working with the flag that the bit has been added to splitOne or not
if(addToSplitOne)
{
binarySplitOne[i] = reversedBinaryValue.get(i);
// now set the split to true so the flag is interchanged, and we can add value in split two
addToSplitOne = false;

} // end of if
else
{
binarySplitTwo[i] = reversedBinaryValue.get(i);

// now set the split to false so that we can add value to split one
addedToSplitOne = true;
} // end of else

} // end of main if

} // end of for
for(int i = 0; i < reversedBinaryValue.size(); i++)
{
boolean addToSplitOne = true;

// since arrays are already filled with zeroes, it skips an additional step of
// filling with zeros
if(reversedBinaryValue.get(i) == 1)
{
// now working with the flag that the bit has been added to splitOne or not
if(addToSplitOne)
{
binarySplitOne[i] = reversedBinaryValue.get(i);
// now set the split to true so the flag is interchanged, and we can add value in split two
addToSplitOne = false;

} // end of if
else
{
binarySplitTwo[i] = reversedBinaryValue.get(i);

// now set the split to false so that we can add value to split one
addedToSplitOne = true;
} // end of else

} // end of main if

} // end of for
14 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @. 𝚂𝚎𝚛𝚊𝚙𝚑𝚒𝚗𝚊 ~ 👠! 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.
MdS
MdS2mo ago
What is your reversedBinaryValue? Would like to test it
. 𝚂𝚎𝚛𝚊𝚙𝚑𝚒𝚗𝚊 ~ 👠
Its any binary value, 0s and 1s Say, 100101 Then x will be 100001 and y will be 000100
MdS
MdS2mo ago
Yeah, but is it in like a List or something? Since you are using .size()
. 𝚂𝚎𝚛𝚊𝚙𝚑𝚒𝚗𝚊 ~ 👠
Oh, its an arraylist
dan1st
dan1st2mo ago
You need to declare boolean addToSplitOne = true; before the beginning of the loop Otherwise you are setting it to true in each iteration
. 𝚂𝚎𝚛𝚊𝚙𝚑𝚒𝚗𝚊 ~ 👠
Before the beginning of the for loop?
dan1st
dan1st2mo ago
yes
. 𝚂𝚎𝚛𝚊𝚙𝚑𝚒𝚗𝚊 ~ 👠
Awesome, thank you. I just woke up, I will check as soon as I am on my PC! That works thank you! But is there any chance you can explain why that didnt work? I would assume that it was due to the block level but it was still a higher block than where it was changing.
dan1st
dan1st2mo ago
that here ah wrong one that loops work by executing one instruction after another so if the boolean addToSplitOne = true; is inside the loop, addToSplitOne = true will be executed in each iteration
. 𝚂𝚎𝚛𝚊𝚙𝚑𝚒𝚗𝚊 ~ 👠
Ah, that makes sense. So basically no matter what, all the control variables must always be declared outside the loops Along with all the flags
dan1st
dan1st2mo ago
depends If you want to keep the values outside of iterations, you can do that
. 𝚂𝚎𝚛𝚊𝚙𝚑𝚒𝚗𝚊 ~ 👠
That does quite make sense Thank you very much for your help, I will be closing the thread now!
JavaBot
JavaBot2mo ago
Post Closed
This post has been closed by <@369486413890060289>.
Want results from more Discord servers?
Add your server