-1

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();
}**
Michael
  • 3,093
  • 7
  • 39
  • 83
Ekenne Chilex
  • 45
  • 2
  • 8

2 Answers2

1

You've put the if outside the while. So you're just comparing val with null.

Put it inside the while, and before buffered.readLine(), so you're not ignoring the first line. Something like this:

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

    line = buffered.readLine();        
}
Dani M
  • 1,173
  • 1
  • 15
  • 43
0

change your code like this

while (line != null) {

        line = buffered.readLine();
        if (val.equalsIgnoreCase(line)) {
            Toast.makeText(this, "Working", Toast.LENGTH_LONG).show();
        }
    }
MaxExplode
  • 1,977
  • 2
  • 17
  • 27
  • in a case I have a statement not just a word in my editText how can I loop through all the words to detect words that exists within the backlist. if (val.equalsIgnoreCase(line)) { for (int i=0;i>val.length();i++){ Toast.makeText(this, "Working", Toast.LENGTH_LONG).show(); } – Ekenne Chilex Nov 30 '16 at 15:28
  • if you are going to match more than just one word then it is good go with a string matching algorithm like rabin carp https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm or look at this post http://stackoverflow.com/a/1765616/4363046 – MaxExplode Nov 30 '16 at 15:35