7 Replies Latest reply: Dec 30, 2007 9:59 AM by 800282

# Convert binary String to One Complement.

Hi All,

this is my first post on here, as I'm really new to Java. I'm first year University in Bradford, England.

Anyways, I've really been struggling with trying to write a program which will convert any binary String to the One's Complement of that String (for anyone who doesn't know, this simply means changing any '1' to '0' and vice versa, ie "110011" would become "001100" etc...(excuse my immense patronising)) I've tried about 6/7 different ways, all the programs compile and run, but never give me the result I want.

My latest attempt is as follows:
``````import java.util.*;
import java.lang.*;

public class OneComplement2 {

public static void main (String [] args) {

String a = "1";
String b = "0";

String binary;
String binary2;
//        int i=0;

System.out.println("Enter a binary number:");

//      public String replaceAll(String regex,
//      String replacement)

binary2 = binary.replaceAll(a, b);// binary2 = binary.replaceAll(b, a);

System.out.println(binary2);

}

}``````
I'm stumped guys. Any help would be much appreciated.

Cheers,

later,

Aaron.
• ###### 1. Re: Convert binary String to One Complement.
Whenever you replace all 1's with 0's, you can change 0's back to 1's since you will change everything to 1's that way.
Here's an approach:
```- replace all 1's to X's
- replace all 0's to 1's
- replace all X's to 0's
- have a beer```
• ###### 2. Re: Convert binary String to One Complement.
``````package crap;

class FormatTest {
public static void main(String[] args) {
int x = 15;
String binary = Integer.toBinaryString(x);
binary = ("00000000"+binary).substring(8-binary.length());
System.out.println(x+" = "+binary);
String complement = binary.replaceAll("0","x").replaceAll("1","0").replaceAll("x","1");
System.out.println("!"+x+" = "+complement);
}
}``````
Who's shout is it?
• ###### 3. Re: Convert binary String to One Complement.
I'll have a cold Duvel Keith.
Cheers!
• ###### 4. Re: Convert binary String to One Complement.
I can't get the head to go through my modem. Damn ADSL. I knew I should have gone for cable.

BTW... Apparently, there's a village in the north of Scotland which had single malt whisky on tap in every house... Whisky has a lower freezing point than water, so it doesn't freeze in the pipes.
• ###### 5. Re: Convert binary String to One Complement.
corlettk wrote:
I can't get the head to go through my modem. Damn ADSL. I knew I should have gone for cable.

BTW... Apparently, there's a village in the north of Scotland which had single malt whisky on tap in every house... Whisky has a lower freezing point than water, so it doesn't freeze in the pipes.
Practical people, those Scots! I like it.
; )
• ###### 6. Re: Convert binary String to One Complement.
Thanks a lot for your help fellas... 'preciate it.

Aaron.
• ###### 7. Re: Convert binary String to One Complement.
Azz wrote:
Thanks a lot for your help fellas... 'preciate it.

Aaron.
No problem Aaron.