0

I have no clue how I would research this otherwise, so here this is. I'm trying to create a program that will "flip bits", e.g. the string "00110011" would return "11001100". I tried to make a for loop to output the individual characters so see if getting the characters would work in this way, but it stops without outputting the characters.

public static void main(String[] args) {
    String bitsList = "01010101";
    char[] sepBits = bitsList.toCharArray();
    System.out.println("Array'd");
    int num = bitsList.length();
    System.out.println("Got length");
    for (int count = 0; count == num;) {
                System.out.println(sepBits[count]);
                System.out.println("Outputted " + sepBits[count]);  
    }
}
1
  • 1
    What do you think condition in for loop decides about? Commented Aug 14, 2015 at 21:24

3 Answers 3

3

You never go in your for loop because count is 0 and num is 8 (length of "01010101"). Therefore count == num evaluates to false and the for loop is not entered.

Try to replace your for loop with:

for (int count = 0 ; count < num ; count++) {
    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

No worries. I hope it helped :)
0

The variable count is not equal to the variable num so the for loop never triggers. I think you are looking for <= not ==. Also you never change the count so the loop will just keep printing the same spot over and over even if you do change this.

Comments

0

this might work for you

public static void main(String []args){
    String bitsList = "01010101";
    char[] sepBits = bitsList.toCharArray();
    int num = bitsList.length();

    for ( int i = num - 1 ; i >= 0 ; i-- ) {
        System.out.println("Outputted " + sepBits[i]);
    }    
}

1 Comment

This just reverses the string. His example is a bit ambiguous, but I think he is asking how to flip the bits, i.e. 0->1, 1->0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.