How to compare user-entered strings with strings already saved in a .txt

1

I'm working with file manipulation and I'm not able to compare what the user types with the password and login saved in the person's registration file! I can not get a specific position within .txt

public class Validacao {
public static Aluno validarlogin() throws IOException {

    Aluno alu = new Aluno();
    Scanner logusuario = new Scanner(System.in);
    System.out.println("Digite seu nome completo: ");
    alu.nome = logusuario.nextLine();
    String nomearq = alu.nome + ".txt";
    BufferedReader br = new BufferedReader(new FileReader(nomearq));
    String linha = ";";

    while ((linha = br.readLine()) != null ){
        String[] vet = nomearq.split(";");
            if(alu.login.equals(vet[1]) && alu.senha.equals(vet[2])){
                System.out.println("Senha correta! ");

                alu.login(vet[2]);
                alu.senha(vet[3]);
            }

    }
    return alu;

}

With this code I get this error:

  

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

But they should have 12 Strings in my .txt

At the time of saving the register I saved separating with ; .

And the split() was to pass the data from .txt to ArrayList was not?

    
asked by anonymous 16.06.2016 / 19:09

1 answer

0

This code:

String[] vet = nomearq.split(";");

Should not it be so?

String[] vet = linha.split(";");

Because it is declared, the variable nomearq is the name of the .txt file, not its content per line

Anyway, it would be easier to see the contents of the .txt file, from what is written, I'm assuming that the contents of it are multiple lines with login and password in type:

algumacoisa;aluno;senha1;outras coisas
algumacoisa;aluno2;1234;outras coisas
algumacoisa;pass;user;outras coisas
algumacoisa;foo;1234;outras coisas

But I think the main mistake is to have switched nomearq to linha yourself, try this and see if it works

    
16.06.2016 / 19:55