Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从读入文本文件中获取单个字符并将其存储到二维数组中?_Java - Fatal编程技术网

Java 如何从读入文本文件中获取单个字符并将其存储到二维数组中?

Java 如何从读入文本文件中获取单个字符并将其存储到二维数组中?,java,Java,我有一个文本文件,看起来像这样: ##oooooo ##oo#oo# oo##o##o oo##o##o o#oo#oo# oo##o##o oo##o##o o#oo#oo# 我想把它插入到一个大小为10x10的二维字符数组中。像这样 ########## ###oooooo# ###oo#oo## #oo##o##o# #oo##o##o# #o#oo#oo## #oo##o##o# #oo##o##o# #o#oo#oo## ########## 下面的代码首先打印出纯“#”的2D数组

我有一个文本文件,看起来像这样:

##oooooo
##oo#oo#
oo##o##o
oo##o##o
o#oo#oo#
oo##o##o
oo##o##o
o#oo#oo#
我想把它插入到一个大小为10x10的二维字符数组中。像这样

##########
###oooooo#
###oo#oo##
#oo##o##o#
#oo##o##o#
#o#oo#oo##
#oo##o##o#
#oo##o##o#
#o#oo#oo##
##########
下面的代码首先打印出纯“#”的2D数组。我继续阅读文本文件,抓取每一行的单个字符,并在I和j位置为它们设置一个char值。这是我到目前为止的代码

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);        
    char[][] hashArray = new char[10][10];
    readFromFile("grid1.txt", hashArray);
}
public static void readFromFile(String txtFile, char[][] hashArray)
{
    // Displays basic 10x10 array of #
    int counter = 0;
    for (int i = 0; i < hashArray.length; i++)
    {
        for (int j = 0; j < hashArray.length; j++)
        {
            if (counter == 10)
            {
                System.out.println();
                counter = 0;
            }
            hashArray[i][j] = '#';
            System.out.print(hashArray[i][j]);
            counter++;
        }
    }
    System.out.println("\n");

    counter = 0;
    for (int i = 0; i < hashArray.length; i++)
    {
        for (int j = 0; j < hashArray.length; j++)
        {
            // I use a counter to skip to next line
            if (counter == 10)
            {
                System.out.println();
                counter = 0;
            }
            // Basically creates my # borders
            if ((i == 0) || (i == 9) || (j == 0) || (j == 9))
            {
                hashArray[i][j] = '#';
                System.out.print(hashArray[i][j]);     
                counter++;
            }
            else
            {
                // counter set at 2 compensates for the length of
                // each line of read in text file.
                counter = 2;
                try
                {
                    Scanner read = new Scanner(new File(txtFile));
                    while (read.hasNext())
                    {                
                        String grid = read.nextLine();
                        char value = grid.charAt(i);
                        hashArray[i][j] = value;
                        System.out.print(hashArray[i][j]);     
                        counter++;
                    }
                }
                catch (FileNotFoundException fnf)
                {
                    System.out.println("File was not found.");
                }
            }
        }
    }
    System.out.println();
}

我感谢所有的帮助。谢谢。

您不就是想在输入的每一行的开头和结尾添加一个哈希标记(#)吗

如果是这样的话,这样不是更容易吗

public static void main(String[] args) throws IOException {
    Files.readAllLines(Paths.get("grid1.txt"))
            .forEach(i -> System.out.println("#" + i + "#"));
}

您的
i
循环从0迭代到9,但字符串只有8个字符长。因此,当你试图访问第9个字符时,它会抛出一个异常。这听起来像是我可以尝试的,老实说,它基于一个基于松散方向的赋值。谢谢你的意见。
public static void main(String[] args) throws IOException {
    Files.readAllLines(Paths.get("grid1.txt"))
            .forEach(i -> System.out.println("#" + i + "#"));
}