0

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.");
        }
Tonantzin
  • 29
  • 2
  • 6
    `number.charAt(i) == 0` You are probably looking to compare char values here eg `number.charAt(i) == '0'` – copeg Jul 06 '16 at 20:36
  • Thank you! That fixed that problem. Do you happen to have any ideas about the second portion--with how I have it now, the end bit determines whether it is true or false. I wanted it to be true only if ALL the bits are 1 or 0, but I can't think of anything. – Tonantzin Jul 06 '16 at 20:42
  • 1
    Stop setting it to `true` when you find a `'1'` or `'0'`. Initialize the boolean to `true` instead of `false`, and only set it to `false` when there's a violation. Never set it back to `true` once it's `false`. – Lew Bloch Jul 06 '16 at 21:49

2 Answers2

2

When checking for char equality, be sure the comparison is what you need. For instance

if(number.charAt(i) == 0)

checks for decimal value equality. To check for an actual '0' char, compare the char value

if ( number.charAt(i) == '0' )
copeg
  • 8,290
  • 19
  • 28
  • Thank you, as well! I'll ask you, too-- Do you have any ideas about the second portion--with how I have it now, the end bit determines whether it is true or false. I wanted it to be true only if ALL the bits are 1 or 0, but I can't think of anything. – Tonantzin Jul 06 '16 at 20:43
  • You can validate the user input in several ways. One might be to count each correct value, then check if this count is equal to the expected length - if so set `zeroesAndOnes ` to true – copeg Jul 06 '16 at 20:45
  • I added the rest of my code above, and I had a similar thought just before I read this to use a count on the false value. But now, no matter what I put in, I always get my "These numbers do not match spec" message. – Tonantzin Jul 06 '16 at 20:53
  • `count` (in the edited version) counts the number of incorrect entries. If all are correct it will be 0. Your code then has the conditional `if(number.length() == 32 && count > 1){` – copeg Jul 06 '16 at 20:57
0

for comparing a char you should use

if(number.chartAt(i) == '0') 

another issue is number.charAt(0) will give you char not int. so when you are doing

number.charAt(0)+number.charAt(1) //you are concatenating character at index 0 and index 1
// do this
int first = Integer.parseInt(number.substring(0,1));
int second = Integer.parseInt(number.substring(1,2));
if( (first+second)%2 == 1){
    // your statement
}
  • I didn't even notice that mistake. Thank you! When I tried to modify that for my for loop, I couldn't make it work: if((symmDiff + number.substring(i, i + 1)) % 2 == 1){ I've never used substring before. It complained about the mod operator in this portion. – Tonantzin Jul 06 '16 at 21:16
  • Wait, I changed that and added for(int i = 2; i < number.length(); i++){ int num1 = Integer.parseInt(number.substring(i,i + 1)); if((symmDiff + num1) % 2 == 1){ ---does that look correct? I think it's working. Still testing. – Tonantzin Jul 06 '16 at 21:21
  • What is `if ((symmDiff + num1) % 2 == 1` all about? From what I can tell, it's equivalent to `if (symmDiff != num1)`. – Klitos Kyriacou Jul 06 '16 at 22:51