import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JFileChooser;

/**
 * @author David Matuszek
 */
public class IntArrayReader {

    /**
     * @param args Unused.
     */
    public static void main(String[] args) {
        while (true) {
            int[][] array = null;
            try {
                array = readIntArray();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            if (array == null) break;
            printIntArray(array);
        }
    }


    /**
     * Asks the user to select a file, then reads in a two-dimensional
     * array of integers from that file. Does not require that all rows
     * of the array be the same length. Returns <code>null</code> if
     * the user cancels the read operation.
     * 
     * @return A two-dimensional array of integers, or <code>null</code>.
     * @throws IOException If there is an error reading the file.
     * @throws NumberFormatException If the file contains something other than integers.
     */
    static int[][] readIntArray() throws IOException, NumberFormatException {
        ArrayList<int[]> list = new ArrayList<int[]>();
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Load which file?");
        int result = chooser.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            if (file != null) {
                String fileName = file.getCanonicalPath();
                BufferedReader myBufferedFileReader =
                    new BufferedReader(new FileReader(fileName));
                while (true) {
                    String line = myBufferedFileReader.readLine();
                    if (line == null || line.trim().equals("")) break;
                    String[] rowValues = line.trim().split(" +");
                    int[] row = new int[rowValues.length];
                    row = new int[rowValues.length];
                    for (int i = 0; i < row.length; i++) {
                        row[i] = Integer.parseInt(rowValues[i]);
                    }
                    list.add(row);
                }
            }
            return list.toArray(new int[0][0]);
        }
        else return null;
    }

    /**
     * @param array
     */
    private static void printIntArray(int[][] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.printf("%5d", array[i][j]);
            }
            System.out.println();
        }
    }

}
