简体   繁体   中英

How do I get individual characters from read in text file and store them into a 2D array?

So i have a text file that looks like this:

##oooooo
##oo#oo#
oo##o##o
oo##o##o
o#oo#oo#
oo##o##o
oo##o##o
o#oo#oo#

I want to insert it in the middle a 2D char array of size 10x10 full of '#'. So something like this.

##########
###oooooo#
###oo#oo##
#oo##o##o#
#oo##o##o#
#o#oo#oo##
#oo##o##o#
#oo##o##o#
#o#oo#oo##
##########

The code below first prints out a 2D array of pure '#'. I proceeded to read in the text file and grab ever individual character of each line and set them a char value at i and j position. And here is my code so far...

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();
}

When I run it, it gives me this error.

java.lang.StringIndexOutOfBoundsException: String index out of range: 8

I appreciate any and all help. Thanks.

Aren't you just essentially trying to prepend and append a hash tag (#) at the start and end of each line in your input?

If so, wouldn't it be easier like this?

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

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