JAVA How do you scan a .txt file and allocate individual characters it to a 2d boolean array using charAt(int)?

334 views Asked by At

First of all, thank you for coming and taking the time to help with the problem.

I am a complete noob (3rd day programming) which explains why I have spent countless head-aching hours trying to solve this problem and still have not figured it out yet:

Problem: How do you scan a .txt file which consists of strings of characters (* and _) and convert it to a boolean array (i.e. * = true and _ = false) (also how do you print it out? I’m guessing we would need a double for loop as well)? The scanner doesn’t seem to work in the main method and I get a ‘no such file’ error.

Bonus Problem: How do you iterate the grids (e.g. using for loop) so that the new grid replaces the old one and the newest grid becomes the new grid non-stop? I'm not familiar with the 'show()' method but I kind of got it to work.

Here's my incomplete code to give you an idea of the problem:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class GameOfLife {

    public static boolean[][] gen() throws IOException {

        Scanner scanner = new Scanner(new File("seed.txt"));
        int rows = 0, columns = 0;
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            if (s.length() > columns)
                columns = s.length();
            rows++;
        }

        boolean[][] grid = new boolean[rows][columns + 1];
        Scanner scanner1 = new Scanner(new File("seed.txt"));
        String line;        
        for (int r = 0; r < rows; r++) {
            line = scanner1.nextLine();
            System.out.println(line);
            for (int c = 0; c <= line.length(); c++) {
                if (r == line.length()) {
                    break;
                }
                if(line.charAt(r) == '*') {
                grid[r][c] = true;
                }
            }
        }
        return grid;
    }

    public static boolean[][] nextGen(boolean[][] cells){
        boolean[][] newCells = new boolean[cells.length][cells[0].length];
        int num;
        for(int r = 0; r < cells.length; r++){
            for(int c = 0; c < cells[0].length; c++){
                num = numNeighbours(cells, r, c);
                if (occupiedNext(num, cells[r][c]))
                    newCells[r][c] = true;
            }
        }
        return newCells;
    }

    public static void main(String[] args) throws IOException{      
        boolean[][] cells = gen();
        show(cells);
        cells = nextGen(cells);
        show(cells);
        Scanner scanner2 = new Scanner(new File("seed.txt"));
        while(scanner2.nextLine().length() != 0){
            cells = nextGen(cells);
            show(cells);
        }
    }

    private static boolean inbounds(boolean[][] cells, int r, int c) {
        return r >= 0 && r < cells.length && c >= 0 &&
        c < cells[0].length;
    }

    private static int numNeighbours(boolean[][] cells, int row, int col) {
        int num = cells[row][col] ? -1 : 0;
        for (int r = row - 1; r <= row + 1; r++)
            for(int c = col - 1; c <= col + 1; c++)
                if (inbounds(cells, r, c) && cells[r][c] )
                    num++;
        return num;
    }

    public static void show(boolean[][] grid){
        String s = "";
        for(boolean[] row : grid){
            for(boolean val : row) 
                if(val)
                    s += "*";
                else
                    s += "_";
            s += "\n";
        }
        System.out.println(s);
    }

    public static boolean occupiedNext(int numNeighbours, boolean occupied){
        if (occupied && (numNeighbours == 2 || numNeighbours == 3))
            return true;
        else if (!occupied && numNeighbours == 3)
            return true;
        else
            return false;
    } 
}
1

There are 1 answers

3
NFE On

I just want to suggest a solution that may help you. Try to read file content and assigned into string variable. Now use string.replaceAll method to (* and _) to * = true and _ = false. Than write updated string into file.