Problem when displaying the contents of an array in a file

1

The problem I have is that I need to save the temperatures of a week in a file and when I do, the file only shows me the last temperature entered.

I do not know where the error is so that it shows me all the temperatures.

public class Principal {

public static void introducirTempe() throws IOException {
    Scanner sc = new Scanner(System.in);
    double dias[];
    //int aux = 0;
    String diasSemana[] = {"Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo"};
    dias = new double[7];
    for (int x = 0; x < diasSemana.length; x++) {
        System.out.println("Introduce la temperatura del " + diasSemana[x]);
        dias[0] = sc.nextDouble();

        FileWriter f = null;
        PrintWriter p = null;
        try {
            f = new FileWriter("Temperaturas.txt");
            p = new PrintWriter(f);

            for (int j = 0; j < dias.length; j++) {
                p.println(dias[j]);
            }
        }catch (IOException e) {
            System.out.println(e.getMessage());
         }finally {
            f.close();
            }
        }
    }

I also leave the main

public static void main(String args[]) throws IOException {
    Scanner sc = new Scanner(System.in);
    int opcion = -1;
    while (opcion != 0) {

        System.out.println("MENU");
        System.out.println("Selecciona una de las opciones ");
        System.out.println("1. Introducir temperatura del dia");
        System.out.println("2. Realizar estadistica");
        System.out.println("3. Salir");
        opcion = sc.nextInt();
        switch (opcion) {
            case 1:
                introducirTempe();
                break;
            case 2:
                //estadistica();
                break;
            case 3:
                //salir();
                break;
        }
    }
}
}
    
asked by Stewie 26.04.2017 в 10:44
source

1 answer

2

You are writing all the time in the same position as array (in position 0):

dias[0] = sc.nextDouble();

If your idea is to introduce the temperature for each day of the week, you should take advantage of your variable x of the loop for to introduce temperatures in array :

dias[x] = sc.nextDouble();
    
answered by 26.04.2017 / 10:49
source