9 Replies - 9719 Views - Last Post: 20 October 2010 - 12:10 AM Rate Topic: -----

#1 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 01:33 PM

For my assignment for class I am suppose to take a text file and make a maze using # = for walls,
blank spaces = movable spaces, a = starting point, * = ending point.
How would I put the already made text file into a 2D array of chars?
Example:
#################
#  a#		#
#   #  #####	#	
#   #	   #	#
#  #	#  #	#
#  #	#  #	#
#  #	#	*
#	####### #
########	#
#		#
#################


This would have to be put into a 2D array of Chars, but I can't just put the width and height because if someone made a maze and you didn't know the width or height the program should be able to do it.
*** The example image isn't showing up how it is suppose to.

This post has been edited by sport10: 19 October 2010 - 01:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Storing Text File into 2D Array of Chars

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,205
  • Joined: 20-September 08

Re: Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 02:31 PM

Use a Scanner and s.nextLine().toCharArray
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 02:32 PM

You could read each line in as a String, and store them in a List<String>. Then create a new char[][] with the first dimension equal to the size() of the List, and the second dimension left blank. So: char[][] c = new char[myList.size()][];. You can then use the String toCharArray() method to convert each String to a char[], and assign each char[] to c[someIndex].
Was This Post Helpful? 0
  • +
  • -

#4 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 05:03 PM

Here's what I got now:
import java.io.*;
import java.util.*;

public class Driver {
	File mapFile = new File("map.txt");
	
	public static void main(String[] args) {
		Driver d = new Driver();
	}
	public Driver(){
		loadMap(mapFile);
	}
	private void loadMap(File f){
		try{
			String line;
			Scanner scan = new Scanner(f);
			char[][] map;
			for(int i =0; scan.hasNextLine();i++){
				line = scan.nextLine();
				map[i][]=line.toCharArray();
				System.out.println(line.length());
			}
		} catch(IOException e){
			System.out.println("INVALID FILE");
		}
	}
}


I'm not too familiar on the toCharArray method. I looked on google for some examples, but there are very few toCharArray examples for 2D arrays. Any Suggestion?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 05:07 PM

Here is a basic example:
char[][] c = new char[2][1];
c[0] = "12345".toCharArray();
c[1] = "23456".toCharArray();



Basically, c[0] holds the array {'1','2','3','4','5'}, and c[1] holds the array {'2','3','4','5','6'}.
Was This Post Helpful? 1
  • +
  • -

#6 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 05:12 PM

Thanks for the response macosxnerd. I appreciate it.
I know how it would hold those examples, but I'm not sure how I would go about doing it for a text file. The above code that I posted doesn't allow me to use the toCharArray method. It says cannot conform char to type char.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 05:14 PM

You need to initialize your 2D array. Also, notice how I assign c[0] an array, not c[0][0] an array. Remember that 2D arrays are an array of arrys. So with have one big array, with each element containing a char[].
Was This Post Helpful? 0
  • +
  • -

#8 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 06:54 PM

I still can't get this thing to store what I need it to. Here is what I have now:
import java.io.*;
import java.util.*;

public class Driver {
	private File mapFile = new File("map.txt");
	private String line;
	private char [][]map;
	
	public static void main(String[] args) {
		Driver d = new Driver();
	}
	public Driver(){
		loadMap(mapFile);
		System.out.println(map[0][1]);
	}


	private void loadMap(File f){
		try{
			int n=0;
			Scanner scan = new Scanner(f);
			while(scan.hasNextLine()){
				line=scan.nextLine();
				n++;
			}
			map = new char[n][];
			for(int i = 0; i < map.length; i++){
				map[i] = scan.nextLine().toCharArray();
			}
		}catch(IOException e){
			System.out.println("INVALID FILE");
		}
	}
} 


Is this not reading it right, because I get a NoSuchElement Error when I compile it. I believe the code scans each line and then the n integer keeps track of each line, then assigns that to the 1st array dimension in the char[][] map array. Then it takes the length and takes the strings of the lines and stores them as chars, which is then put into the 2nd element of the 2d array. What am I doing wrong?
Was This Post Helpful? 0
  • +
  • -

#9 sport10  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 07:25 PM

Am I even close on this or am I doing it completely wrong?
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,205
  • Joined: 20-September 08

Re: Storing Text File into 2D Array of Chars

Posted 20 October 2010 - 12:10 AM

You didn't initialize the array. You need something more like

    public static char[][] loadMap(File f) throws IOException {
        Scanner s = null;
        char[][] result = new char[64][];

        try {
            s = new Scanner(f);

            int i = 0;

            while (s.hasNextLine()) {
                result[i++] = s.nextLine().toCharArray();
            }

            s.close();
            result = (char[][]) CompactArray.compact(result);
            // Above method here: http://technojeeves.com/joomla/index.php/free/66-compact-array-in-java
            // DEBUG - print it
            System.out.println(Arrays.deepToString(result));

            return result;
        } finally {
            try {
                s.close();
            } catch (Exception e) { /* ignore */
            }
        }
    }


This post has been edited by g00se: 20 October 2010 - 12:12 AM
Reason for edit:: Formatting buggy (still)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1