user11200571
user11200571

Reputation:

Storing doubles into a 2D array

I'm having trouble storing numbers from a text file into a 2 dimensional array. The first number represents the number of rows and the second number represents the number of columns.The values from the text file are:

3 3 
13.3 14 
9.9 1.7
0.343 2 6.44234
0.1234 798

It's successfully storing the first 2 numbers as the rows and columns, but gives me this output after running:

3
3
Error discovered at 0,0: java.lang.ArrayIndexOutOfBoundsException: 0
Error discovered at 0,1: java.lang.ArrayIndexOutOfBoundsException: 0
Error discovered at 0,2: java.lang.ArrayIndexOutOfBoundsException: 0
Error discovered at 1,0: java.lang.ArrayIndexOutOfBoundsException: 1
Error discovered at 1,1: java.lang.ArrayIndexOutOfBoundsException: 1
Error discovered at 1,2: java.lang.ArrayIndexOutOfBoundsException: 1
Error discovered at 2,0: java.lang.ArrayIndexOutOfBoundsException: 2
Error discovered at 2,1: java.lang.ArrayIndexOutOfBoundsException: 2
Error discovered at 2,2: java.lang.ArrayIndexOutOfBoundsException: 2

I'm not sure what the issue is but here is the code I am using:

public class twoDArray 
{

    public static void main(String[] args) throws IOException
    {
    String filename = JOptionPane.showInputDialog("Please enter the file name: ");
    File file = new File(filename);
    try 
    {
        Scanner inputFile = new Scanner(file);
        inputFile.close();
    } 
    catch (FileNotFoundException e) 
    {
        JOptionPane.showMessageDialog(null, "File does not exist", "error", 1);
        System.exit(0);
    } 
    int ROW_SIZE = 0;
    int COLUMN_SIZE = 0;
    double[][] TEST_ARRAY = new double[ROW_SIZE][COLUMN_SIZE]; 
    loadArray(filename, ROW_SIZE, COLUMN_SIZE, TEST_ARRAY);

public static void loadArray(String file, int ROW_SIZE, int COLUMN_SIZE, double[][] TEST_ARRAY) throws FileNotFoundException{
    Scanner inputFile = new Scanner(new File(file));
    while(inputFile.hasNextInt()) {
        ROW_SIZE = inputFile.nextInt();
        COLUMN_SIZE = inputFile.nextInt();
    }
    System.out.println(ROW_SIZE);
    System.out.println(COLUMN_SIZE);
     for (int i = 0; i < ROW_SIZE; i++) {
            for (int j = 0; j <  COLUMN_SIZE; j++) {
                try {
                    if (inputFile.hasNextDouble()){
                        TEST_ARRAY[i][j] = inputFile.nextDouble();
                    }
                } 
                catch (Exception e) 
                {
                    System.err.println("Error discovered at " + i + "," + j + ": " + e);
                }
            }
     }

    inputFile.close();

}
}

Any help or guidance would be highly appreciated as I've already spent 7+ hours on debugging and trying to figure out a solution.

Upvotes: 0

Views: 40

Answers (1)

John Kugelman
John Kugelman

Reputation: 361749

int ROW_SIZE = 0;
int COLUMN_SIZE = 0;
double[][] TEST_ARRAY = new double[ROW_SIZE][COLUMN_SIZE]; 

ROW_SIZE and COLUMN_SIZE are both 0 at this point. It's allocating a 0x0 array. You need to delay calling new until after you've read in the two sizes. Or you need to read the sizes earlier. One of the two.

Upvotes: 1

Related Questions