4 Replies - 8277 Views - Last Post: 01 March 2011 - 09:21 PM Rate Topic: ***-- 2 Votes

#1 shipwills  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-March 11

Read in file and store in 2D array

Posted 01 March 2011 - 07:50 PM

I'm trying to read in a txt file with no spaces.
An example would be:
27
77
100000100001010000100001000010
100010101010010101010100000000

with 27 rows and 77 columns of 1's and 0's. The instructions are
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

I believe i've done all of them but, read the rest of the file, storing each value in the appropriate position in the cells array. I know i need to use inFile.nextLine and inFile.hasNext to determine if their are any more rows to read in the array. I've tried a while loop and for loops but can't seem to put it all together. If you guys/gals have any suggestions that'd be great. I've searched for answers online for quite sometime now. At one point I got the array to print all the lines and columns but everything was a 0 repeating.


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 {
		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];

                //this is were I need help
		for(int i=0; i < rows; i++) 
		{
			String line = inFile.nextLine();
			line = line.substring(0);

		}
			
		inFile.close();
		
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				System.out.print(cells[i][j]);
			}
			System.out.print();
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Read in file and store in 2D array

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Read in file and store in 2D array

Posted 01 March 2011 - 08:02 PM

For each row read the line containing the data which you are doing.
With a nested for() loop use the charAt method of the Strings class to copy each character from the line. The Character class has a method, digit, that will convert the character to an integer.

This post has been edited by n8wxs: 01 March 2011 - 08:14 PM

Was This Post Helpful? 0
  • +
  • -

#3 shipwills  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-March 11

Re: Read in file and store in 2D array

Posted 01 March 2011 - 08:34 PM

I'm not allowed to use a StringTokenizer or use charAt to get each digit.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Read in file and store in 2D array

Posted 01 March 2011 - 08:49 PM

So read the line as a String and

char[] digit = line.toCharArray();
then you can fill the cell[ofThatRow][col] with each digit[i] - '0'
Was This Post Helpful? 0
  • +
  • -

#5 shipwills  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-March 11

Re: Read in file and store in 2D array

Posted 01 March 2011 - 09:21 PM

That's not making a lot of sense to me.

This is the whole assignment.. and a new explanation.


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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1