Trying to read data file into a 2D Array and print to screen

335 views Asked by At

I'm trying to read data from a file and load into a two dimensional array and then print it to the screen.
but i keep receiving error code

Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
    at test.main(test.java:14)

Here is my code below. Any advice would be great. I Feel like im missing something stupid and just need another pair of helpful eyes here. thanks

import java.io.*;
import java.util.*;

public class test{ 

    public static void main(String args[]) throws IOException{ 
        double [][] P= new double [5][3];

        BufferedReader Infile = new BufferedReader(new FileReader("P08RATE.DAT"));
        StringTokenizer Tokens;
        while (Infile.ready()){ 
            Tokens = new StringTokenizer(Infile.readLine());
            for (int k=0; k<P.length; k++)
                for (int j=0; j<P[k].length; j++)  
                    P[k][j] = Double.parseDouble(Tokens.nextToken());
        }
        Show(P);
    }

    /*-------------- Method: Show() ---------------*/
    private static void Show(double C[][]){ 
        int i, j;
        for (i=0; i < C.length; i++){ 
            for (j=0; j < C[i].length; j++)  
                System.out.print("  " + C[i][j]);
            System.out.println('\n');  
        }
    } //PrintArray

}
1

There are 1 answers

1
Reinstate Monica Please On

It's not completely clear what you're trying to do without seeing the input file. But if it's strictly something like

double double double
double double double
double double double
double double double
double double double

You should have the line

Tokens = new StringTokenizer(Infile.readLine());

Inside of your first for loop.