ekenne chilex ekenne chilex - 8 days ago 7
Android Question

Compare values between a .txt file and editText

I am trying to create a blacklist for my app, so any word that exists in the blacklist.txt file is inputted by user via editText will be flagged out. The code I have below is not doing anything and I don't know why.

private String comparevalue() {
BufferedReader buffered = null;

String val = editbox.getText().toString();
InputStream inputStream =getResources().openRawResource(R.raw.blacklist);
buffered = new BufferedReader(new InputStreamReader(inputStream));

try {
String line = buffered.readLine();
while (line != null) {

line = buffered.readLine();

}if (val.equalsIgnoreCase(line)) {
Toast.makeText(this, "Working", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this,"Not found",Toast.LENGTH_LONG).show();
**}
return buffered.toString();
}**

Answer

change your code like this

while (line != null) {

        line = buffered.readLine();
        if (val.equalsIgnoreCase(line)) {
            Toast.makeText(this, "Working", Toast.LENGTH_LONG).show();
        }
    }