File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Map text file to a 2 dimensional array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Map text file to a 2 dimensional array." Watch "Map text file to a 2 dimensional array." New topic

Map text file to a 2 dimensional array.

Jonathan Haybok

Joined: Mar 12, 2009
Posts: 16
All I want to do is store patterns that are imported from a text file. The text file would be simple asterisks that would signify an alive cell (cell with a filled background). Spaces would be just be cells that are not alive. I'm sure many of you know about the Game of Life (I've been told it's the most programmed game), and that's what I'm doing.

I have a grid that I create with 2d arrays. I fill cells in the grid by using another 2d array that just stores true/false values. So I just need to figure out how to map an input stream of character/spaces to indexes of an array.

Can anyone help me out with this? I'm having trouble figuring out how the input stream identifies characters, and how, for example, to extract the values of an asterisk that would correspond to array[0][0]. What equates to rows/columns in text files? I apologize ahead of time for my poor description of the problem. Hopefully someone understands it.

I know I will have to modify this block of code:
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

A good place to start is the IO Chapter of the Java Tutorial. It explains what functionality is available in the API. I'm sure you'll find some features that make your job fairly simple.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Map text file to a 2 dimensional array.
It's not a secret anymore!