-2

The problem I have is at line 21, which is input[Weeks][Results] = scanner.nextDouble();.

I dont know what I'm doing wrong. The program just basically ask me for how many weeks i want to input and how many results per week and then store them in a 2d array.

package com.company;

import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("How many weeks?: ");
        int Weeks = scanner.nextInt();
        System.out.print("How many results per week?: ");
        int Results = scanner.nextInt();

        double[][] input = new double[Weeks][Results];

        for (int i = 0; i < Weeks; i++) {
            System.out.println("Temperature Week " + Weeks + ": ");
            for (int k = 0; k < Results; k++) {
                input[Weeks][Results] = scanner.nextDouble();
            }
        }
    }
}                                                                  
Community
  • 1
  • 1

2 Answers2

0

You are trying to insert the data at the same position you have declared as maximum length.

Your array has a size of [Weeks][Results] and you are trying to add a double in [Weeks][Results] position into loop.

Change values to i and k like this:

input[i][k] = scanner.nextDouble(); 

Also your print line into loop should be:

System.out.println("Temperature Week " + i + ": ");

That's not an error, but the print will output always the number of weeks instead of the actual week you want to insert the temperature.

J.F.
  • 13,927
  • 9
  • 27
  • 65
0

Use input[i][k] = scanner.nextDouble(); instead of input[Weeks][Results] = scanner.nextDouble();

Programmer
  • 803
  • 1
  • 6
  • 13