public class Car { public static void main(String[] args) { String river = "Mississippi" ; String encoding = river.replace("i", "!"); river.replace("s", "$"); System.out.println("Encoding: " + encoding) ; System.out.println("Expect: M!$$!$$!pp!") ; } } when i run it it only changes the i with !, but not the s with $ what did i do wrong? :S
Your teacher was clever to give you a task that hasn't been documented like the normal String river = "Mississippi" projects don't you need to nest multiple replace calls?
Sorry for all the spam! But, instead of: String encoding = river.replace("i", "!"); river.replace("s", "$"); Try: String encoding = river.replace("i", "!").replace("s", "$"); Or you can try: String encoding = river.replace("i", "!"); encoding = encoding.replace("s", "$"); Hopefully it should be what you're looking for! I think what was happening was that you were redeclaring river so it would reset to Mississippi each time, and then perform the replace.
I'm by no means a Java expert, I prefer other languages. But I'll give it a crack: Original code: public class Car { public static void main(String[] args) { String river = "Mississippi" ; String encoding = river.replace("i", "!"); river.replace("s", "$"); System.out.println("Encoding: " + encoding) ; System.out.println("Expect: M!$$!$$!pp!") ; } } Now, this can't be the complete code, I see a few flaws in both syntax and logic. You've left out the definition of the encoding string. Next you are trying to make the replacements update on the river string, which won't work number one, and number two, makes no sense since you output the encode string, not the original river string. And last, the .replace operation do not change the string it operates on, it creates a new value, that can either be output directly ( eg: System.out.println(somestring.replace(x,y)); ) or be transfered to another preferably empty string. Anyhow, what should work is this: public class Zylark_Rules{ public static void main(String[] args){ //definitions String river = "Mississippi"; String encoding = ""; //even an empty string needs to be told to be empty... String encoding2 = ""; //operations encoding = river.replace("i", "!"); /* the string river is not changed, but the result is stored in the string encoding, which we operate on and transfer to a new string in the next line... */ encoding2 = encoding.replace("s", "$"); //output System.out.println("Original: " + river); //just to verify that original is not changed System.out.println("First replace: " + encoding); //to see result from first replace System.out.println("Final replace: " + encoding2); //...and second replace System.out.println("Expect: M!$$!$$!pp!"); }//end main }//end class Code tidied up a bit, with comments. Something you should always use, no matter the size of the project. Helps when debugging, trust me. Now, I do not have a Java compiler, so I can't test it. Tell me how it goes edit, you could also nest the transfer, like optikal did. Though then you won't see in the output what happens at each step. That may not be important, naturally, and such nesting sure makes for tidier code, as you can dispense with the encode2 string. It would then look like this: public class Zylark_Rules{ public static void main(String[] args){ //definitions String river = "Mississippi"; String encoding = ""; //even an empty string needs to be told to be empty... //operations encoding = river.replace("i", "!"); /* the string river is not changed, but the result is stored in the string encoding, which we operate on and transfer to the same string in the next line... */ encoding = encoding.replace("s", "$"); //output System.out.println("Original: " + river); //just to verify that original is not changed System.out.println("Encoding: " + encoding); //to see result from both replacements System.out.println("Expect: M!$$!$$!pp!"); }//end main }//end class
I know Zylark already posted working code, but the part I added in bold is a minor correction showing where you went wrong. When you replaced the i with the !, you set the string named 'Encoding' to store the result. When you replaced the s with the $, you didn't do anything with it. You used the replace function but didn't do anything with the output from that function.