简体   繁体   中英

Read 2d Array and Transpose

So the purpose of this code is to read a 2d array from a file and transpose it. I know the transpose portion works but I am having trouble reading the array from the file. I continue to get an array out of bounds error. Any help would be great! Thanks!

Here is the code and the data file is at the bottom.

import java.util.*;
import java.io.*;
public class prog464d{
public static void main(String args[]) throws IOException{
    final int ROWS = 5;
    final int COLS = 5;

    int[][] nums = new int[ROWS][COLS];
    // this is used only in java 7 (not java 6)
    try (Scanner input = new Scanner(new File("prog464adat.txt"))) {
        int row = -1; // since we're incrementing row at the start of the loop
        while(input.hasNext()) {
            row++;
            String[] line = input.nextLine().split("\t");
            for(int col = 0; col < COLS; col++) {
                try {
                    nums[row][col] = Integer.parseInt(line[col]);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (FileNotFoundException e) {
        // do something here
        System.out.print("File not found!");
        e.printStackTrace();
    }

    for (int i = 0; i < nums.length; i++) {
        for (int j = 0; j < nums[i].length; j++) {
            System.out.print(nums[i][j] + " ");
        }
        System.out.print("\n");
    }
    System.out.print("\n\n matrix transpose:\n");
    // transpose
    if (nums.length > 0) {
        for (int i = 0; i < nums[0].length; i++) {
            for (int j = 0; j < nums.length; j++) {
                System.out.print(nums[j][i] + " ");
            }
            System.out.print("\n");
        }
    }
}

}

Data file:

45 67 89 12 -3
-3 -6 -7 -4 -9
96 81 -8 52 12
14 -7 72 29 -1
19 43 28 63 87

You are doing split on read line from the file using \\t but actually i copied your data file and checked it is having only space not \\t.

so replace this line

 String[] line = input.nextLine().split("\t");

to

 String[] line = input.nextLine().split(" ");

when we put split based on \\t then line variable gets complete first line of a file as there is no tab in your data file.

so line becomes 45 67 89 12 -3

so nums[row][col] = Integer.parseInt(line[col]); line is doing parsing of integer but

actual line is 45 67 89 12 -3 and it has spaces , so it will throw number format exception .

since it is handled using catch block it goes to next exception

Array index bound exception

as you are trying to access array but no element in it because of Number format exception.

Instead of Scanner class, use BufferReader class to read a file line by line. When you read each line, save it into Array or ArrayList of int. When you read the whole file, convert Array or ArrayList to 2D array.

For example:

Reading a file line by line and save each line into an array or arrayList

File file = new File(filename);
    FileReader reader = new FileReader(file);
    BufferedReader buffReader = new BufferedReader(reader);
    String line = buffReader.readLine();
    while(line!=null){
       lines.add(line);
       line = buffReader.readLine();

    }

convert array or arraylist to 2D dimensional array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM