Search in java file

Asked

Viewed 44 times

-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. You could 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");
    }
}

That’s the code part. It should read line by line from login.txt, through the split separate the line into parameters of a vector, and compare the input with one of these parameters that is already defined. It seems to me that he is not passing the other lines and is only reading the first, when I "seek" the one of the first line, he returns perfectly, the one of the others not... why?

1 answer

1


Cocoa, how are you? Your question is not very clear, please write more details. But before hand:

1) check if the file path is correct

2) Your Divider vector at position 1, does it 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;
                }
}
  • It seems to me that he is not reading the next lines. Because then, he will read a.txt file right? He’s supposed to go line by line, but it seems to me he stops at the first one and doesn’t go any further. When I put it in main, if it’s from the first line, it returns, if it’s not, it returns nothing, it’s as if it doesn’t jump to the next line. Sorry, it’s the first comment I make here haha and thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.