Search code examples
androidfilebufferedreader

checking for a set of string in a text file android


I am trying to check if all of the strings in tr are present in file output. If not then I should return true so that I can notify the user, but something is wrong: I am getting the notification again and again even if all strings are present in the file .

 public boolean checkinfile(File output, String[] tr) throws IOException {
    Boolean flag = false;
    BufferedReader br = new BufferedReader(new FileReader(output));
    String read = br.readLine();
    int x = 0;
    for (x = 0; x < tr.length; x++) {
        if ((read.contains(tr[x]))) {

        }
            else {
            flag = true;
            return flag;
        }
    }
    return flag;
}

Solution

  • try this code : your code read only the first line of file , in my suggestion , when it reads the file , if a string not present in the file ,break the loop of reading and return true:

     public boolean checkinfile(File output, String[] tr) throws IOException {
        Boolean flag = false;
        BufferedReader br = new BufferedReader(new FileReader(output));
    
        for (int x = 0; x < tr.length; x++) {
            String read = br.readLine();
            if (!(read.contains(tr[x]))) {
                flag=true;
                break;
            }
        }
         return flag;
    
    }