2

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?

James
  • 23
  • 4
  • 2
    "why it is not working as it should". What should it output? – M A Nov 12 '15 at 07:55
  • Write the string down on paper. Manually replace everything. You'll see.. `replaceAll` replaces all the text from the first parameter with the text from the second parameter. Read http://docs.oracle.com/javase/7/docs/api/java/lang/String.html for information. – Christophe De Troyer Nov 12 '15 at 07:55
  • You should note that you are passing an argument to the method, but overwrite it inside the method with another String. Perhaps that's causing your confusion. – Eran Nov 12 '15 at 07:57
  • Your method (after closing the String literal in the first line), returns `& , &,& , 2 , 2,2 , U ,U , U, 4 ,4 , 4,,,,,,,,,,` – Eran Nov 12 '15 at 07:59
  • This string is supossed to return `& , &,& , 2 , 2,2 , U ,U , U, 4 ,4 , 4,,,,,,,,,,,`but the problem is that it returns `and` instead – James Nov 12 '15 at 08:03
  • @James - This code is *unreadable*. You have so many replaceAll statements. Very few people will actually try to understand what you are trying to do. Try editing the question and put only the code which is relevant and readable – TheLostMind Nov 12 '15 at 08:05
  • 1
    @James you have to provide the code, which uses this method, because `simplifier()` returns exactly what you need – Stanislav Nov 12 '15 at 08:19

2 Answers2

1

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);
Arash
  • 825
  • 2
  • 11
  • 19
0

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.

Joel Min
  • 3,387
  • 3
  • 19
  • 38
  • It Doesn't seem to work, when i inputted `and asdfasdf`, it still outputted `and and` – James Nov 12 '15 at 08:40
  • I tried your suggestion, and it still did not work. It seems as the return method stops when it meets a white space in the string, beause when i inputted `and hello`, it returned `&`, and when i inputted ` and hello`, `and` got returned – James Nov 12 '15 at 08:45