In this segment of code, from everything I'm seeing, it should be entering the for
loop and then the if
statement as long as you enter 1's and 0's, which is what I'm doing. It's not entering it, as I've seen from my print statements.
I don't see a reason why.
If it did enter the if
statement, I also am unsure what to do because my suspicion is that it will only set true
if the last bit is not a 1 or 0: my intention being for zeroesAndOnes
to be false
if anything except 1's and 0's are entered. However, as it stands, it's false
all the time.
System.out.println("Please enter a 32 bit number consisting of "
+ "1's and 0's.");
String number = kb.nextLine();
int count = 0;
boolean zeroesAndOnes = false;
for(int i = 0; i < number.length(); i++){
if(number.charAt(i) == '0' || number.charAt(i) == '1'){
zeroesAndOnes = true;
System.out.println("If boolean " + zeroesAndOnes);
}
else{
zeroesAndOnes = false;
count++;
}
}
System.out.println("If boolean end " + zeroesAndOnes);
if(number.length() == 32 && count > 1){
if(number.charAt(0) + number.charAt(1) % 2 == 1){
symmDiff = 1;
}
else{
symmDiff = 0;
}
for(int i = 2; i < number.length(); i++){
if((symmDiff + number.charAt(i)) % 2 == 1){
symmDiff = 1;
}
else{
symmDiff = 0;
}
}
System.out.println("The parity bit for this number is " + symmDiff);
}
else{
System.out.println("These numbers do not match the specification.");
}