Search in java file

-3

The teacher passed an activity, which is for you to put the files in a txt, read, change and delete ... I am programming the search function, and it is not working. Could you help me?

public void searchUser(String login){
        String result = "";
        try {
        FileReader fr = new FileReader("C:\Users\vanes\Documents\WEBprog\Archive\login.txt");
        BufferedReader br = new BufferedReader(fr);
        while (br.ready()) {
            result += br.readLine() + "\n";
            String divider[] = new String[2];//split                
            divider = result.split(";"); //split
            if(divider[1].equals(login)){
                System.out.println(divider[1]);
                break;
            }
        }
        br.close();
        fr.close();
    } catch (Exception ex) {
        System.out.println("Erro");
    }
}

This is the snippet of code. It should read line by line from login.txt, by splitting the split line into a vector's parameters, and comparing the input with one of those parameters that is already defined. It seems to me that he is not going through the other lines and is only reading the first one, when I "look" for the first line, it returns perfectly, the other does not ... why?     

asked by anonymous 31.08.2018 / 18:41

1 answer

1

Cocoa, all good? Your question is not very clear, please write more details. But from ante ante:

1) Make sure the file path is correct

2) Does your divider vector in position 1 have any value? if it is null it will give an error in the equals method. do it here

if(diveder[1] != null) {
    if(divider[1].equals(login)){
                    System.out.println(divider[1]);
                    break;
                }
}
    
31.08.2018 / 18:59