0

I want the user to input a String word by phrase,

"animal" is a null String and I want to get it when the user enters the words after it or before it, I want to get that single word at its current position.

i.e.:

when I enter in my EditText "cat is an animal" the word "cat" will be saved in the list of animals, or "dog is an animal" and the word "dog" is saved, then the TextView says 'change to "ok"'.

public class MainActivity extends AppCompatActivity {

ArrayList<String> animal_names = new ArrayList<>();
String animal = null;
TextView say;
EditText enter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    say = (TextView) findViewById(R.id.textView);
    enter = (EditText) findViewById(R.id.editText);


    try {
        InputStream input = getAssets().open("animals names.txt");
        InputStreamReader rd = new InputStreamReader(input);
        BufferedReader br = new BufferedReader(rd);
        String line;
        while ((line = br.readLine()) != null) {
            animal_names.add(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }


}

public void click(View view) {
    String s = enter.getText().toString().trim();
    if (s.equals(animal+" is an animal")){
        say.setText("ok");
        animal_names.add(animal);
    }else if (s.equals("is " + animal + "an animal" )){
        if (animal_names.contains(animal)){
            say.setText("yes");
        }else {
            say.setText("no");
        }
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

0

use text watcher and when the users string equals animal,,,save those letters in a new string inside text watcher

Ashith VL
  • 89
  • 3
  • 14
0

Unable to understand when you want to perform that action. Anyway what did i understand from your question is you need to add TextWatcher() listner to your EditText and compare your String inside its afterTextChanged() methods. Its look like:

enter.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                 String s1 = s.toString().trim();
                 if (s1.equals(animal+" is an animal")){
                   say.setText("ok");
                   animal_names.add(animal);
                 }else if (s1.equals("is " + animal + "an animal" )){
                   if (animal_names.contains(animal)){
                      say.setText("yes");
                   }else {
                      say.setText("no");
                   }
                 }
            }
        }); 

----------Updated-----------------

try this inside afterTextChanged() or click of any other Button, add other conditions too if you require.

if (s.endsWith("is an animal")) {// for "XXX is an animal" condition
            animal_names.add(s.substring(0, s.indexOf("is")).trim());
            say.setText("yes");
    }else if (s.contains("is")&&s.endsWith("an animal")) {// For "is XXX an animal" condition
       say.setText("yes");
       animal_names.add(s.substring(s.lastIndexOf("is")+2, s.indexOf("an animal")).trim());
    }else{
       say.setText("no");
    }

Hope it help!!! or update your question if you requirement is something else.

Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41
0

Currently your code checks for the strings null is an animal and is nullan animal (notice your spacing typo?)

So, hard to say if you really need a TextWatcher, but probably a regex instead. For example, a simple regex could be

"Is ([A-Za-z ]+) an animal?" 

And

"([A-Za-z ]+) is an animal" 

Then, you would use if (s.matches(regex)), and see how to get your animal

How to extract a substring using regex


You can also use a combination of substring and indexOf to extract the animal name

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245