简体   繁体   中英

How do you take a 2D array from a text file, and “copy” it into another array using charAt

I was working through a project and I have to use charAt to continuously add elements of an array from a text file to a new array that i would have specified. The size of the array differs depending on the text file being used so it is best to assume that the contents of the file are unknown, however i will provide an example.

I keep on getting a "StringIndexOutOfBoundException" when i run my code and i am not sure why, or how to fix it.

What the code should be doing is taking the user input to get the exact text file location, then it will be reading that line by line and adding that to a new array. The first two lines of the text file array are the array row and column size.

my code is as follows:

public static void main(String[] args) throws FileNotFoundException
    {
    System.out.println("Enter the location of the board file using the FULL PATH NAME.");
    Scanner input = new Scanner(System.in);
    String n = input.nextLine();
    input.close();

    File a = new File(n); 

    Scanner sc = new Scanner(a);
    int row = sc.nextInt();
    int col = sc.nextInt();

    char[][] board = new char[row][col];

    for (int numRow = 0; numRow < row+1; numRow ++)
    {
        String string = sc.next();
        for (int numCol = 0; numCol < col+1; numCol++)
        {
            board[row][col] = string.charAt(numCol);   
        }
    }

    sc.close();

    GridGame game = new GridGame (row, col, board);
    game.playGame();

An example input text file:

10 10

EEEEEEEEES

SWWWWWWWWW

EEEEEEEEES

SWWWWWWWWW

EEEEEEEEES

SWWWWWWWWW

EEEEEEEEES

SWWWWWWWWW

EEEEEEEEES

TWWWWWWWWW

The issue you are facing is because a Java array has 0-based index ie the index of the first element is 0 and that of the last element is array.length - 1 . Therefore, your loop with the variable as the index of the array should not go beyond array.length - 1 . Your terminating condition, numRow < row+1 is taking the value of numRow up to the length of the array instead of array.length - 1 .

Replace

for (int numRow = 0; numRow < row+1; numRow ++)
{
    String string = sc.next();
    for (int numCol = 0; numCol < col+1; numCol++)
    {
        board[row][col] = string.charAt(numCol);   
    }
}

with

for (int r = 0; r < row && sc.hasNextLine(); r++) {
    board[r] = sc.nextLine().toCharArray();
}

The other concept you need to understand is a 2-D array in Java an array of arrays ie board[0] should hold the array of characters from the first line of the file, board[1] should hold the array of characters from the second line of the file and so on. Now, if you want to access the 4th character of the 3rd line from the file, you can access it as board[2][3] .

The last but not the least is regarding closing the Scanner for System.in . You should never close this Scanner because it also closes the System.in . So, you should remove the line, input.close() from your code.

Feel free to comment in case of any doubt/issue.

[Update]

You can write the above mentioned single loop as a nested loop as well but it is unnecessary.

for (int r = 0; r < row && sc.hasNextLine(); r++) {
    char[] lineChars = sc.nextLine().toCharArray();
    for(int c = 0; c < col; c++) {
        board[r][c] = lineChars [c];
    }
}

You need a nested loop in order to access/process individual characters but to store/access/process each row of a 2-D array, you do not need a nested loop. As I have already mentioned, a 2-D array is an array of 1-D arrays and therefore to access each of these 1-D arrays (not individual elements inside these 1-D arrays), you need a single loop, not a nested loop.

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