I want to know how I can replace 0 with 1 and 1 with 0 in a string in java,
so if I have
String x = "10010"
I want String y = "01101"
and I want to do it for any String so if i have a longer one like 010000010 it would do the same with no issues
I tried doing .replace but then it became 11111 or 00000.
import java.util.Arrays;
import java.util.Scanner;
public class allTest {
public static void main(String[] args)
{
String x = "010000010";
String temp1=x.replace('0', 'x');
//temporary variable for replace
String temp2=temp1.replace('1',
'0');
String new_str=temp2.replace('x',
'1');
System.out.println(new_str);
}
}
Get Answers For Free
Most questions answered within 1 hours.