Question

I want to know how I can replace 0 with 1 and 1 with 0 in...

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.

Homework Answers

Answer #1

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);
      
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I am having issues with a Backus Naur assignment I want to define rules that allow...
I am having issues with a Backus Naur assignment I want to define rules that allow for an integer named x to have one of the following: 0-F followed by “h” to define a hex value, 0-1 to define a binary value, or 0-9 followed by a “d” to define a decimal value. For example, int x = 3d would be a legal statement On the other hand, int x= 4b would not be legal (binary numbers can only be...
In Java is there a way I can put a value in a linkList = array[...
In Java is there a way I can put a value in a linkList = array[ ] string (I don't know how to word it here what I have so far with my code) * Example object 51 I want to equal Array [0]; // Project code below public String getCardRank() { String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; return "RRR"; } // this part is in another .java file private...
Hello my name is Nartlada, i would like to know how can i create the histograms...
Hello my name is Nartlada, i would like to know how can i create the histograms from the number of the excel file. The question is asking "create histograms illustrating the distributions of the two temperature variables (T1 and T2). correctly the label the x and y axes and give your plot title. We are required to use R program that include the set working directory. I tried to drag the excel file to here or even drag just the...
let me know how the CoViD-19 pandemic is affecting you. I mainly want to get a...
let me know how the CoViD-19 pandemic is affecting you. I mainly want to get a sense of how well you can focus on the materials and how you are doing so far.
I am interested in doing a survey. I want to know the average GPA of WVU...
I am interested in doing a survey. I want to know the average GPA of WVU students within .1. I want to be 95% certain. I know the variance of GPAs is .09. How many students do I need to survey? A. 25 B. 30 C. 35 D. 40
Java program question! If I pass null to a method, how can I use the passed...
Java program question! If I pass null to a method, how can I use the passed value to compare. Here is example code, it might be wrong, please make it could run on eclipse. Public class hw{ public static void main (String args[]) { method1(); } private static void method1() { System.out.println(method(null)); } public static String method(int[] a) { return null; } What I want to get in the output is just "()". It is not just to type the...
how i can calculate RB angles if i dont know where is laying but i know...
how i can calculate RB angles if i dont know where is laying but i know the vertical and WCB angles however i do not have any graph
I have the correct answer but I want to know how to solve it step by...
I have the correct answer but I want to know how to solve it step by step, please help. Thanks ID X Y A 2 6 B 5 4 C 8 5 D 6 2 E 9 4 1) What is the obtained F-ratio? (correct answer: 0.474) 2) What can you conclude with ANOVA? (correct answer: Fail to reject the null, p> 0.01; Type II error is possible) 3) What is the standard deviation of Y for the sample? (correct...
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
How can I alter this Java method so that I convert and then return every sentence...
How can I alter this Java method so that I convert and then return every sentence in the array of strings instead of just the first sentence? public static char[] charConvert ( String [] sentences ) { int totLength = sentences[0].length() + sentences[1].length() + sentences[2].length() + sentences[3].length() + sentences[4].length(); char [] letter = new char[totLength]; int x = 0; int y = 0;    while ( y < sentences[x].length() ) { switch ( sentences[x].charAt(y) ) { case 'a': case 'A':...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT