Results 1 to 2 of 2
  1. #1
    coyne20 is offline Member
    Join Date
    Feb 2009
    Rep Power

    Default How do I read each character from a file and write it to each index of an array?

    Hi all

    What I need to do in essence is to read a file that contains the following pattern as shown on the attachment to store each character of the pattern to an index of a multi-dimensional array.

    I would like some suggestions on how to go about constructing an nxn array to store the various characters in java.

    your suggestions would be greatly appreciated and welcomed.
    Attached Files Attached Files
    Last edited by coyne20; 10-09-2011 at 04:17 PM.

  2. #2
    Join Date
    Jul 2011
    Rep Power

    Default Re: How do I read each character from a file and write it to each index of an array?

    I've written this code, hope this helps:

    Java Code:
    import java.util.*;
    public class SlimeTorpedo {
    	public static void main(String[]args) {
    		File file = new File("SlimeTorpedo.txt");
    		ArrayList<String> arrayList = new ArrayList<String>();
    		try {
    			//reads each line from the text file and places it into a string array
    			BufferedReader br = new BufferedReader(new FileReader(file));
    			String str;
    			while ((str = br.readLine()) != null) {
    			//creates a stringFormat array and places the strings in the array list onto the stringFormat array
    			String stringFormat[] = new String[arrayList.size()];
    			//creates 2d char array and writes the characters from the stringFormat array onto the 2d char array
    			char charFormat[][] = new char[stringFormat.length][];
    			for (int i = 0; i < stringFormat.length; i++) {
    				charFormat[i] = stringFormat[i].toCharArray();
    			//for testing purposes, prints the 2d charFormat array
    			for (int i = 0; i < stringFormat.length; i++) {
    				for (int j = 0; j < charFormat[i].length; j++) {
    		catch (FileNotFoundException ex) {
    		catch (IOException ex) {
    There may be a way to jump straight into the 2d char array but I've never tried. I thought this was the best way to solve the problem.

    For info on ArrayLists check here: ArrayList (Java Platform SE 6)

    For info on 2d char arrays check here: Java: Arrays -- 2-dimensional

    Regards Serb.
    Last edited by SerbianSergeant; 10-09-2011 at 07:01 PM.

Similar Threads

  1. how to read array index
    By aconti in forum New To Java
    Replies: 5
    Last Post: 08-12-2011, 09:52 PM
  2. How to read the word character in image file.
    By sureshsoftengg in forum Java Software
    Replies: 1
    Last Post: 09-21-2010, 03:37 PM
  3. read the word character in image file
    By sureshsoftengg in forum Advanced Java
    Replies: 2
    Last Post: 09-21-2010, 03:36 PM
  4. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  5. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 12:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts