I\'m supposed to input a string, and replace all and
, to
, you
, and for
substrings with &
, 2
,
Replace the first rope = rope.replace(" and "," & ");
with rope = rope.replace("and "," & ");
Now it should work. The problem was that the first "and" you were trying to replace was and
, not and
, which is why that was left and did not get replaced.
Also remove the second last line of simplifier
, which is System.out.print(rope);
. This is duplicate as you are already printing the result in the calling method.
UPDATE: I see what you are trying to do here. Try this:
For each word that you want to replace, replace it only once. So for and
, do:
rope.replace("and", "&");
For to
, do:
rope.replace("to", "2");
DO NOT add any space between the words, it is not necessary. Doing replace()
once will replace ALL occurrences of that word.
Here is how I simplified your code and got the correct result:
String rope = "and , and,and , to , to,to , you ,you , you, for ,for , for,a , a,e , e,i , i,o , o,u , u";
// rope = rope.replaceAll(" ", "");
rope = rope.replaceAll("and", "&");
rope = rope.replaceAll("to", "2");
rope = rope.replaceAll("you", "U");
rope = rope.replaceAll("for", "4");
rope = rope.replaceAll("a", "");
rope = rope.replaceAll("e", "");
rope = rope.replaceAll("i", "");
rope = rope.replaceAll("o", "");
rope = rope.replaceAll("u", "");
System.out.println(rope);