I'm very new and need help on right now reading into the array. Im confused at theString line = inFile.nextLine(); for(int i=0; i < line.length(); i++) { int digit = Integer.parseInt(line.substring(i,i+1));
part. I know this isn't right because im getting errors but I'm not really sure were to go from here. I'm not suppose to you a Tokenizer or charAt to get the digit. The file i'm reading in looks like this ruffly with 27 rows and 77 columns of 1's and 0's.
27
77
00000000000000000000
00010100000001000000
01000010010000100000
/** import java.io.*; import javax.swing.*; import java.util.*; import java.awt.*; public class Proj4 { public static int rows, cols; public static int[][] cells; /** * main reads the file and starts * the graphical display */ public static void main(String[] args) throws IOException { //Read the file //Store the number of rows in the "rows" variable, and the number of columns //in the "cols" variable //create the cells array to be rows x cols size //read the rest of the file, storing each value in the appropriate position in //the cells array Scanner s = new Scanner(System.in); String file = JOptionPane.showInputDialog(null, "Enter the input file name: "); Scanner inFile = new Scanner(new File(file)); rows = Integer.parseInt(inFile.nextLine()); cols = Integer.parseInt(inFile.nextLine()); cells = new int[rows][cols]; String line = inFile.nextLine(); for(int i=0; i < line.length(); i++) { int digit = Integer.parseInt(line.substring(i,i+1)); } inFile.close(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { System.out.print(cells[i][j]); } System.out.println(); } //This line starts the graphical portion -- leave it here! Board b = new Board(); } /** * update finds the next generation of the cells */ public static void update() { int[][] nextGen = new int[rows][cols]; //YOU DO THIS //update the cells array following the Game of Life rules //you should not update any element in the first or last row, or //first or last column //HINT: you might want to build a second array with the new values THIS IS WHERE CELLS DIE OR COME TO LIFE //now, copy values from nextGen into cells } /** * getCells creates a String array of the current cells, * using *'s for live cells * * @return The current generation of cells */ public static String[][] getCells() { String ret[][]; //YOU DO THIS //Make ret be a new String array for your board //It should be the same size as the cells array //Initialize ret to represent the contents of the cells array. If a //spot in the cells array has a 1, put a "*" at that spot in ret. If //a spot in the cells array has a 0, put a " " at that spot in ret. update(); return ret; } } /** * Board handles the GUI for the game of Life * * @author Julie Thornton * @version CIS 200, Project 4 */ class Board { private JFrame frame; private JLabel[][] labels; private int rows, cols; /** * The frame is created, and the timer is started */ public Board() { frame = new JFrame(); frame.setTitle("The Game of Life"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String[][] cells = Proj4.getCells(); rows = cells.length; cols = cells[0].length; frame.setSize(cols*10, rows*10); Container c = frame.getContentPane(); c.setLayout(new GridLayout(rows, cols)); labels = new JLabel[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { labels[i][j] = new JLabel(cells[i][j]); c.add(labels[i][j]); } } java.util.Timer t = new java.util.Timer(); t.schedule(new LifeTask(), 300, 300); frame.setVisible(true); } private class LifeTask extends java.util.TimerTask { /** * run gets the next generation of cells, and updates * the frame so that generation is displayed. */ public void run() { String[][] cells = Proj4.getCells(); for (int i = 0; i < cells.length; i++) { for (int j = 0; j < cells[0].length; j++) { labels[i][j].setText(cells[i][j]); } } } } }