0

Hi, so I am supposed to read the lines from a text file, and output the data into a 2D array, I have read the lines but I am confused as to how to input the contents to the 2D array.

This is the file :

eoksibaebl
ropeneapop
mbrflaoyrm
gciarrauna
utmorapply    
wnarmupnke 
ngrelclene 
alytueyuei 
fgrammarib 
tdcebykxka

My problem is how do I put these strings into the the 2d array as shown below.

public class WordFinder {
    public static final int N = 10;
    public static char[][] grid = new char[N][N];
    public static final String GRID_FILE = "grid.txt";

    public static void initGrid() {
        try {
            File file = new File(GRID_FILE);
            Scanner scanner = new Scanner(file);
            while (scanner.hasNext()) {
                System.out.println(scanner.next());
            }
            scanner.close();
            }
        catch (FileNotFoundException e) {
            System.out.println("File not found.");
        }
    }

I am quite new to JAVA, so any help would be much appriciated!

Grim
  • 4,939
  • 8
  • 46
  • 97
  • https://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java and https://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array – ryanlutgen Aug 20 '18 at 04:17
  • The goal is to end up with `grid[0][0] == 'w'`, `grid[0][1] == 'n'`,..., `grid[0][9] == 'e'`, `grid[1][0] == 'n'`, `grid[1][1] == 'g'`, ..., on up to `grid[9][9] == 'a'`? – Kevin Anderson Aug 20 '18 at 04:22
  • @KevinAnderson yes exactly, this is only half of the text file however, so a would be grid[4][9] –  Aug 20 '18 at 04:35
  • Is there a particular reason for using a primitive array? Your file is 5x10 rather than 10x10, so you'll have a bunch of empty spots in the array. I can post a primitive array solution if you want, but how flexible are you in terms of the design? – ggorlen Aug 20 '18 at 04:37
  • @ggorlen Sorry I should have made it more clear, I have 10 lines in the the text file, I just put these 5 in as an example as I didn't want the post to be too long! –  Aug 20 '18 at 04:40

1 Answers1

0

Here's a quick modification to your code to make it work. Read a line as a string, then iterate over the characters in the string and place them in the char[][] array.

import java.util.*;
import java.io.*;

public class WordFinder {
    public static final int N = 10;
    public static char[][] grid = new char[N][N];
    public static final String GRID_FILE = "grid.txt";

    public static void initGrid() {
        try {
            File file = new File(GRID_FILE);
            Scanner scanner = new Scanner(file);

            for (int i = 0; scanner.hasNext(); i++) {
                String line = scanner.next();

                for (int j = 0; j < N; j++) {
                    grid[i][j] = line.charAt(j);
                }
            }

            scanner.close();
        }
        catch (FileNotFoundException e) {
            System.out.println("File not found.");
        }
    }

    public static void main(String[] args) {
        initGrid();

        for (char[] row : grid) {
            for (char cell : row) {
                System.out.print(cell);
            }

            System.out.println();
        }
    }
}

Output:

eoksibaebl
ropeneapop
mbrflaoyrm
gciarrauna
utmorapply
wnarmupnke
ngrelclene
alytueyuei
fgrammarib
tdcebykxka

Careful, though: this design can crash on input files that are something other than a 10x10 grid of chars. Consider using an ArrayList to dynamically match the size of your text file, or at least you may add to your error handling:

catch (FileNotFoundException | ArrayIndexOutOfBoundsException e) {
    System.out.println("Something terrible happened.");
    System.exit(1);
}
ggorlen
  • 26,337
  • 5
  • 34
  • 50