Recording Files in Java

0

I need to write student names and notes, follow the code ( switch is for implementation of other functions in the future).

I enter the class (with the directory and name of the text file), the name of the student and his notes and save them in the matrix and in the vector, but at the time of writing them in the file, only the first line that was is there. For example: John [4.0.5.0.6.0.7.0, average].

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());
                    }

                }
        }
    }
}
    
asked by anonymous 29.06.2017 / 22:47

1 answer

0

You are only printing the first element because you close your FileWriter after the first entry in if, in this 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 typing everything you want, ie outside the two for loops.

    
30.06.2017 / 15:24