I'm supposed to input a string, and replace all and
, to
, you
, and for
substrings with &
, 2
, U
, and 4
.
When I input the string "and , and,and , to , to,to , you ,you , you, for ,for , for,a , a,e , e,i , i,o , o,u , u"
, it only outputs and
when I print it.
public void simplify()
{
System.out.println("Enter a string to simplify: ");
String rope = in.next();
System.out.println(simplifier(rope));
}
public String simplifier(String rope)
{
rope = rope.replace(" and "," & ");
rope = rope.replace(" and"," &");
rope = rope.replace("and ","& ");
rope = rope.replace(" to "," 2 ");
rope = rope.replace(" to"," 2");
rope = rope.replace("to ","2 ");
rope = rope.replace(" you "," U ");
rope = rope.replace("you ","U ");
rope = rope.replace(" you"," U");
rope = rope.replace(" for "," 4 ");
rope = rope.replace("for ","4 ");
rope = rope.replace(" for"," 4");
rope = rope.replace("a ","");
rope = rope.replace(" a","");
rope = rope.replace("e ","");
rope = rope.replace(" e","");
rope = rope.replace("i ","");
rope = rope.replace(" i","");
rope = rope.replace(" o","");
rope = rope.replace("o ","");
rope = rope.replace("u ","");
rope = rope.replace(" u","");
System.out.print(rope);
return rope;
}
Output:and and
It seems to cut off the returned string after the first space
I have no idea what is going on and why it is not working as it should. What am I doing wrong?