-1

I have written this code to replace "is" with "was". I am not able to do so for more than one word. Can anyone please help me do so?

    public class MainActivity extends AppCompatActivity {
    EditText editText1, editText2 ;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText1 = (EditText)findViewById(R.id.editText);
        editText2 = (EditText)findViewById(R.id.editText2);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String input = editText1.getText().toString();
                String replace =  input.replaceAll("is", "was");
                editText2.setText(replace);
            }
        });
`    }
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
R.ven
  • 23
  • 3

3 Answers3

1

for the rough way but it work for me ! for the example, what u get from edittext1 is "this is me !"

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String input = editText1.getText().toString();
            String replace =  input.replaceAll("is", "was");
            //now you get "this was me!"
            String replace2 = replace.replaceAll("this", "that");
            //now you get "that was me!"
            String replace3 = replace2.replaceAll("me", "yours");
            //at last your replace3 are now is "that was yours!"
            editText2.setText(replace3);
            //editText2 will now display "that was yours!"
        }
    });

I know this a rough way to do so, but it work well for me to replace more than one word:) good luck

MellisaNg
  • 61
  • 7
  • Thanks a lot:), this definitely works for me. But I want to replace words other than this. This code will work perfectly for replacing "is", "this","me". But if i enter a sentence that contains none of these words? I want an idea for that.TIA – R.ven Dec 30 '15 at 04:19
1

If you want to do it in single statement you can use this method

String strOutput = inString.replaceAll("call me","cm").replaceAll("as soon as possible","asap");

If you have many replacements, you better go with this approach

//array to hold replacements
String[][] replacements = {{"is", "was"}, 
                           {"this", "that"}};

//loop over the array and replace
String strOutput = inString;
for(String[] replacement: replacements) {
    strOutput = strOutput.replace(replacement[0], replacement[1]);
}

System.out.println(strOutput);

you can choose any of the method based on your requirement.

droidev
  • 7,352
  • 11
  • 62
  • 94
0

You have to manually create a mapping for this functionality. Like create two arrays, one for present verb and one for past verbs. When you encounter any verb from present array, get its position by looping and matching through present array and then replace that from the verb of past array. Keep the indexes of grammatically opposite verbs same in both arrays . That way you will be able to do that. For eg .

present array =[is,this]

past array=[was,that]

Now if your sentence contains 'is' get its index i.e. 0 and replace it with 0th position string from past array

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84