Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Read in file and store in 2D array start of The Game of Life

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Read in file and store in 2D array start of The Game of Life

    I'm very new and need help on right now reading into the array. Im confused at the
    String 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]);
    				}
    			}
    		}
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: Read in file and store in 2D array start of The Game of Life

    What are these "errors" you're getting? We can't run your code as we don't have access to that file, so it's important that you post the actual exceptions and stack traces you're getting. Otherwise we're just guessing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read in file and store in 2D array start of The Game of Life

    I know it has to do with the part i cut out and posted. I don't really understand how to read in the array without using Tokenizer or charAt. But this is what I get.

    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Proj4.main(Proj4.java:35)

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: Read in file and store in 2D array start of The Game of Life

    If I were you, I'd use some print statements (or better yet, a debugger) to figure out what's going on that makes your input String blank. What index are you at? What line are you one? Try to find out as much information as posisble.

    I would also suggest you take this one step at a time- if you're having trouble with inputting integers, the code you post should not include any code that does not deal directly with that. In other words, it should be in the form of an SSCCE.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 08:13 PM
  2. Store Values in 2D array and output to textarea
    By raverz1tildawn in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 7th, 2011, 02:13 PM
  3. Game of Life GUI Error
    By Lavace in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 3rd, 2011, 08:15 AM
  4. help help, ... :) "game of life"
    By sanfor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2009, 11:40 AM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM

Tags for this Thread