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");
}
}
}