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

    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 05:17 PM.

  2. #2
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    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.io.*;
    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) {
    				arrayList.add(str);
    			}
    			
    			//creates a stringFormat array and places the strings in the array list onto the stringFormat array
    			String stringFormat[] = new String[arrayList.size()];
    			arrayList.toArray(stringFormat);
    			
    			//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++) {
    					System.out.print(charFormat[i][j]);
    				}
    				System.out.println("");
    			}
    		} 
    		catch (FileNotFoundException ex) {
    			ex.printStackTrace();
    		} 
    		catch (IOException ex) {
    			ex.printStackTrace();
    		}
    	}
    }
    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 08:01 PM.

Similar Threads

  1. how to read array index
    By aconti in forum New To Java
    Replies: 5
    Last Post: 08-12-2011, 10: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, 04:37 PM
  3. read the word character in image file
    By sureshsoftengg in forum Advanced Java
    Replies: 2
    Last Post: 09-21-2010, 04:36 PM
  4. Replies: 5
    Last Post: 02-05-2009, 11:28 AM
  5. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 01: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
  •