Read Java File with delimiter

Asked

Viewed 423 times

1

I have the following txt file: 38461;Rui Brito;20 34561;Rui Brito;10 31231;Rui Brito;6

And the following code:

ArrayList<Aluno> listaAlunos = new ArrayList<Aluno>();

public void lerFicheiroPauta(String nomeFicheiro) {
    try {
        Scanner sc = new Scanner(new File(nomeFicheiro));
        sc.useDelimiter("\\; || \n");
        while (sc.hasNext()) {
            int número = sc.nextInt();
            String nome = sc.next();
            int nota = sc.nextInt();
            sc.nextLine();

            listaAlunos.add(new Aluno(número, nome, nota));

        }
        sc.close();
    } catch (FileNotFoundException e) {
        System.out.println("Erro na leitura");
        // listaAlunos.clear();
        e.printStackTrace();
    }

}

public String toString() {
    String sc = "";
    for (Aluno a : listaAlunos) {
        sc += a + System.lineSeparator();
    }
    return sc;

}

What happens is that the result is this: Resultado da execução

Can someone give me a help to read the file properly sff?

  • What do you intend to do? Separate the values between a point and a comma?

No answers

Browser other questions tagged

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