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

huangapple go评论136阅读模式
英文:

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

问题

以下是翻译好的部分:

我正在处理一个项目,我必须使用 charAt 方法从文本文件中连续地将数组的元素添加到我指定的新数组中。数组的大小取决于所使用的文本文件,因此最好假设文件的内容是未知的,不过我会提供一个示例。

当我运行我的代码时,我一直得到一个 "StringIndexOutOfBoundsException" 错误,我不确定为什么会出现这个错误,也不知道如何修复它。

代码应该做的是接受用户输入,以获取精确的文本文件位置,然后逐行读取文件并将其添加到新数组中。文本文件数组的前两行是数组的行和列大小。

我的代码如下:

public static void main(String[] args) throws FileNotFoundException
{
    System.out.println("输入使用完整路径名的板文件位置。");
    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();
}

示例输入文本文件:

10 10

EEEEEEEEES

SWWWWWWWWW

EEEEEEEEES

SWWWWWWWWW

EEEEEEEEES

SWWWWWWWWW

EEEEEEEEES

SWWWWWWWWW

EEEEEEEEES

TWWWWWWWWW
英文:

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(&quot;Enter the location of the board file using the FULL PATH NAME.&quot;);
    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 &lt; row+1; numRow ++)
    {
        String string = sc.next();
        for (int numCol = 0; numCol &lt; 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

答案1

得分: 0

你遇到的问题是因为Java的array具有从0开始的索引,即第一个元素的索引是0,最后一个元素的索引是array.length - 1。因此,你的循环中,将变量作为array的索引,不应该超过array.length - 1。你的终止条件numRow &lt; row+1numRow的值带到了array的长度,而不是array.length - 1

将以下代码段:

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

替换为:

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

你需要理解的另一个概念是,Java中的二维数组是数组的数组,即board[0]应该保存文件第一行的字符数组,board[1]应该保存文件第二行的字符数组,依此类推。现在,如果你想访问文件的第三行中的第四个字符,你可以通过board[2][3]来访问。

最后但同样重要的是关于关闭用于System.inScanner。你不应该关闭这个Scanner,因为它也会关闭System.in。所以,你应该从代码中移除input.close()这一行。

更新

你可以将上述提到的单循环写成嵌套循环,但这是不必要的。

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

你需要一个嵌套循环来访问/处理单个字符,但为了存储/访问/处理二维数组的每一行,你不需要嵌套循环。正如我已经提到的,二维数组是一维数组的数组,因此要访问这些一维数组中的每一个(而不是其中的单个元素),你只需要一个单一的循环,而不是嵌套循环。

英文:

The issue you are facing is because a Java array has 0-based index i.e. 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 &lt; 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 &lt; row+1; numRow ++)
{
    String string = sc.next();
    for (int numCol = 0; numCol &lt; col+1; numCol++)
    {
        board[row][col] = string.charAt(numCol);   
    }
}

with

for (int r = 0; r &lt; row &amp;&amp; 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 i.e. 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.

Update

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

for (int r = 0; r &lt; row &amp;&amp; sc.hasNextLine(); r++) {
	char[] lineChars = sc.nextLine().toCharArray();
	for(int c = 0; c &lt; 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.

huangapple
  • 本文由 发表于 2020年4月6日 04:07:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61048778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定