Writing files to Java

Asked

Viewed 99 times

0

I need to record students' names and grades, follow the code (the switch is to implement other functions in the future).

I insert the class (with the directory and name of the text file), the student’s name and his notes and store them in the matrix and the array, but when writing them in the file, only the first line that was typed is there. For example: John [4.0,5.0,6.0,7.0,media].

public static void main(String[] args) {
    String nomes[] = new String[100];
    double notas[][] = new double[100][5];
    String resposta = "S";
    double media = 0;
    double soma = 0;
    String turma = "";
    String resposta1 = "s";
    Scanner input = new Scanner(System.in);
    System.out.println("O que deseja fazer? LANCAR notas, ver NOTAS, ver RESULTADOS, SAIR?");
    String opcao = input.nextLine();
    while (resposta.equalsIgnoreCase("s")) {
        switch (opcao) {
            case "lancar":
                while (resposta1.equalsIgnoreCase("S")) {
                    for (int i = 0; i <= nomes.length; i++) {
                        System.out.println("Qual o nome da turma?");
                        turma = input.nextLine();
                        input = new Scanner(System.in);
                        System.out.println("Qual o nome do aluno?");
                        nomes[i] = input.nextLine();
                        for (int j = 0; j <= 3; j++) {
                            System.out.println("Qual a nota " + j);
                            notas[i][j] = input.nextDouble();
                            soma = soma + notas[i][j];
                            media = soma / 4;
                            notas[i][4] = media;
                        }
                        System.out.println("Deseja lançar notas de outro aluno? S/N?");
                        resposta1 = input.next();
                        media = 0;
                        soma = 0;
                        if (resposta1.equalsIgnoreCase("n")) {
                            break;
                        }
                    }
                    try {
                        File medias = new File(turma);
                        FileWriter gravador = new FileWriter(medias, true);
                        for (int p = 0; p <= nomes.length; p++) {
                            for (int q = 0; q < 4; q++) {
                                if (notas[p][q] > 0.0) {
                                    gravador.write(nomes[p]);
                                    gravador.write(Arrays.toString(notas[p]));
                                    System.out.printf(String.format("Arquivo %s gravado com sucesso!", turma));
                                    gravador.flush();
                                    gravador.close();
                                }
                            }
                        }
                    } catch (IOException e) {
                        System.err.printf("Erro na gravação do arquivo: %s.\n", e.getMessage());
                    }

                }
        }
    }
}

1 answer

0

Printing only the first element because you close your Filewriter after the first if entry in that part here:

FileWriter gravador = new FileWriter(medias, true);
for (int p = 0; p <= nomes.length; p++) {
    for (int q = 0; q < 4; q++) {
        if (notas[p][q] > 0.0) {
            gravador.write(nomes[p]);
            gravador.write(Arrays.toString(notas[p]));
            System.out.printf(String.format("Arquivo %s gravado com sucesso!", turma));
            gravador.flush();
            gravador.close(); // Erro nesta parte
        }
    }
}

You should close your Filewriter after writing everything you want, that is outside the two loops for.

Browser other questions tagged

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