How to compare user-typed strings to Strings already saved in a . txt

Asked

Viewed 672 times

0

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’t get a specific position inside the .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 there should be 12 strings in mine .txt

At the time of saving the record I saved by separating with ;.

And the split() was to pass the data of the .txt for a ArrayList wasn’t?

  • Show your txt content. The error reported is because you are accessing an invalid vector index vet.

1 answer

0

This code:

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

it shouldn’t be like this?

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

because as stated, the variable nomearq is the file name . txt, not its content per line

Anyway, it would be easier to see the contents of the file. txt, from what is written, I am assuming that its contents are several lines with login and password in the 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 that I switched nomearq for linha even, test this and see if it works

  • Hi I changed name by line and gave the same error! registration was saved in . txt like this: name; login; password; .... and I need to get the login and password of this line (positions 1 and 2) to compare with what was typed

  • please post the contents of your . txt so we can better assess the reason for the error

  • Txt content: Ary;Ary;123456;789456;1;987654;av. Rio amazonas;09/05/1990;Information Systems;5°[TPIII, Networks I, Database I];

  • txt is all saved in a single line separated by ";". there is a better way to do this?

  • @Mayuri You say everything is on one line, all that? All the information? Is there only one line in txt? I believe your mistake, after changing the nomearq.split(";"); for linha.split(";"); is that the information of the object Aluno are not filled, login and senha in case, try to set a login and password before the while and then make the condition.

Browser other questions tagged

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