数组重复添加相同的字符?

Array adds the same character repeatedly?

我试图打印出从文本文件中读取的点和 X 的网格,但出于某种原因,它只需要一个点并将其一遍又一遍地重复添加到数组中,而不是读取每个单独的字符并将其添加到数组中各自的位置。我该如何解决这个问题?

代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Project4 {

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in); // Created a scanner
        System.out.println("Enter the file name you would like to use");
        File file = new File(input.nextLine()); // Takes file name to find file
        Scanner inputFromFile = new Scanner(file);
        FileInputStream fileInput = new FileInputStream(file); // reads file
        int r;
        while ((r = fileInput.read()) != -1) { // goes through each character in
                                                // file, char by char
            char c = (char) r;
            for (int i = 0; i < 25; i++) {
                for (int y = 0; y < 75 ; y++) { // Adds file values to Array
                    GameOfLife.grid[i][y] = c;
                }
            }
        }
        // Prints the initial environment
        System.out.println("Initial set: ");
        for (int j = 0; j < GameOfLife.grid.length; j++)
            System.out.println(GameOfLife.grid[j]);
    }
}

生命游戏:

import java.util.Arrays; 

public class GameOfLife {

static final int m = 25; // number of rows
static final int n = 75; // number of columns 
static char[][] grid = new char [m][n]; // Creates an empty (no dots or X's)grid of rows and columns. 

}

例如:

预期输出:

考虑这个区块

    while ((r = fileInput.read()) != -1) { // goes through each character in
                                            // file, char by char
        char c = (char) r;
        for (int i = 0; i < 25; i++) {
            for (int y = 0; y < 75 ; y++) { // Adds file values to Array
                GameOfLife.grid[i][y] = c;
            }
        }
    }

每次您读取一个字符时,它都会遍历并用该字符覆盖每个单元格,这意味着它只会包含最后读取的任何字符。

如果您想在阅读时跟踪您的坐标,请删除这样的 for 循环

    int y = 0;
    int i = 0;
    while ((r = fileInput.read()) != -1) { // goes through each character in
                                            // file, char by char
        char c = (char) r;
        GameOfLife.grid[i][y] = c;
        y++;
        if (y == 75)
        {
            y = 0;
            ++i;
            if (i == 25)
            {
                break;
            }
        }
    }

中央 for 循环就是问题所在。如前所述,您将读取的每个字符存储在二维数组的每个位置。您也可以这样实现它:

    FileInputStream fileInput = new FileInputStream(file); // reads file
    int r;                                                 // file, char by char
    for (int i = 0; i < 25; i++) {
        for (int y = 0; y < 75 ; y++) { // Adds file values to Array
            r = fileInput.read()) != -1; // goes through each character in
            if( r == -1 ) break;
            char c = (char) r;
            GameOfLife.grid[i][y] = c;
        }
    }

哪个更好,我不确定。这种方式保留了数组嵌套循环的熟悉结构,但如果文件输入结束,它可能会提前退出,如果输入确实意外结束,这可能会导致错误。

您正在用每个字符填充整个数组,因为您嵌套了循环。我认为您应该将 grid 初始化为 '.'(因为它看起来是空的)。

不需要单独的 class 来包含您的数组,除非您打算将更多功能移入其中,否则我会将其设为局部变量(但所有内容都不应该 static).我建议您使用 try-with-resources to avoid leaking file handles. Next, I'd use the Scanner to read the file one line at a time (skipping empty lines and invalid characters). And, you can use Arrays.deepToString(Object[]) 打印您的 grid。像

final int m = 25;
final int n = 75;
char[][] grid = new char[m][n];
for (char[] arr : grid) { // <-- start with an empty grid.
    Arrays.fill(arr, '.');
}
Scanner input = new Scanner(System.in);
System.out.println("Enter the file name you would like to use");
File file = new File(input.nextLine());
try (Scanner inputFromFile = new Scanner(file)) {
    int row = 0;
    while (inputFromFile.hasNextLine()) {
        String line = inputFromFile.nextLine().trim();
        if (line.isEmpty()) {
            continue;
        }
        for (int i = 0; i < n; i++) {
            char ch = i < line.length() ? line.charAt(i) : ' ';
            if (ch != '.' && ch != 'x') {
                continue;
            }
            grid[row][i] = ch;
        }
        row++;
    }
    // Prints the initial environment
    System.out.println("Initial set: ");
    System.out.println(Arrays.deepToString(grid));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}