Reading a value from textfile and comparing with edittext

102 Views Asked by At

I want to compare the value of edit text with all the lines in a file.(the file is word list) .

i have done this ,

        try{
            final InputStream file = getAssets().open("words.txt");
            reader = new BufferedReader(new InputStreamReader(file));
            String line = reader.readLine();
            while(line != null){

                line = reader.readLine();
                if(ss == line.toLowerCase()){
                    Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                            .show();
                }
                else{
                    Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        } catch(IOException ioe){
            ioe.printStackTrace();
        }

here ss is the value of textfield.

 String ss = (res.getText()).toString();

this is the words.txt file https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt

But the above code is not working.

EDIT :

I have checked whether the file is opening or not, the problem is file is not opening.

        try{
            final InputStream file = getAssets().open("words.txt");
            Toast.makeText(this, "File Opened", Toast.LENGTH_SHORT)
                    .show();
            reader = new BufferedReader(new InputStreamReader(file));
            String line ;
            while((line = reader.readLine()) != null){

                if(ss.equalsIgnoreCase(line)){
                    Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                            .show();
                    TextView tv = myTextViewList.get(counter);
                    tv.setText(line);
                }
                else{
                    Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        } catch(IOException ioe){
            ioe.printStackTrace();
        }
4

There are 4 best solutions below

0
On

Try this

ss.equalsIgnoreCase(line.toLowerCase())

Replace "==" either with "equalsIgnoreCase" or "equals"

2
On

You are reading the line twice as well as use equalsIgnoreCase to compare strings

        while((line = reader.readLine()) != null){

            if(ss.equalsIgnoreCase(line)){
                Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                        .show();
            }
            else{
                Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                        .show();
            }
        }
0
On

In your code its continuously compare line by line till it would reach to your last line (i.e. 235886) and it will runs continuously in loop. Its checking all lines one by one so when you compared first time and if it succeed then break the loop, and also use .equals for string comparison rather than '=='

String ss = "a";
        try{
            final InputStream file = getAssets().open("words.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(file));
            String line = reader.readLine();
            while(line != null){

                line = reader.readLine();
                if(ss.equals(line)){
                    Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                            .show();
                    break;
                }
                else{
                    Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                            .show();
                    break;
                }
            }
        } catch(IOException ioe){
            ioe.printStackTrace();
        } 

Hope this will help you..

0
On

Try this one...

BufferedReader in = new BufferedReader(new FileReader(filepath));
boolean found = false;
while (( line = in.readLine()) != null)
{
if (line.contains(str))
{
    found = true;
    break; //break out of loop now
}
 }
 in.close();

if (found)
{
    System.out.println("Yes");
}
else
{
  System.out.println("No");
BufferedWriter out = new BufferedWriter(new FileWriter(filepath,true));
out.newLine();
out.write(str);
out.close();
}