How to Read a Text File of Char to an 2d Array in Java

9 Replies - 22246 Views - Final Post: 20 October 2010 - 12:10 AM Rate Topic: - - - - -

#1

  • D.I.C Head

Reputation: 0

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

Storing Text File into 2D Array of Chars

Posted xix 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 fabricated text file into a 2nd array of chars?
Example:

################# #  a#		# #   #  #####	#	 #   #	   #	# #  #	#  #	# #  #	#  #	# #  #	#	* #	####### # ########	# #		# #################                

This would have to be put into a second assortment of Chars, but I tin't but put the width and elevation because if someone made a maze and you didn't know the width or height the program should be able to practice it.
*** The example image isn't showing up how it is suppose to.

This mail service has been edited by sport10: 19 Oct 2010 - 01:36 PM


Is This A Skillful Question/Topic? 0

  • +

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

#two g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 02:31 PM

Apply a Scanner and south.nextLine().toCharArray


#three macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Storing Text File into 2D Assortment of Chars

Posted 19 October 2010 - 02:32 PM

You could read each line in as a String, and store them in a Listing<String>. So 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 so use the String toCharArray() method to catechumen each String to a char[], and assign each char[] to c[someIndex].


#iv sport10 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 coffee.io.*; import java.util.*;  public class Driver { 	File mapFile = new File("map.txt"); 	 	public static void main(String[] args) { 		Driver d = new Commuter(); 	} 	public Commuter(){ 		loadMap(mapFile); 	} 	private void loadMap(File f){ 		endeavor{ 			String line; 			Scanner scan = new Scanner(f); 			char[][] map; 			for(int i =0; scan.hasNextLine();i++){ 				line = scan.nextLine(); 				map[i][]=line.toCharArray(); 				Organization.out.println(line.length()); 			} 		} catch(IOException e){ 			Arrangement.out.println("INVALID FILE"); 		} 	} }                

I'm not too familiar on the toCharArray method. I looked on google for some examples, but in that location are very few toCharArray examples for 2D arrays. Any Suggestion?


#5 macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Storing Text File into 2nd Array of Chars

Posted 19 October 2010 - 05:07 PM

Here is a basic example:

char[][] c = new char[2][i]; c[0] = "12345".toCharArray(); c[i] = "23456".toCharArray();                

Basically, c[0] holds the array {'1','2','3','four','5'}, and c[1] holds the array {'2','3','4','5','6'}.


#6 sport10 User is offline

  • D.I.C Head

Reputation: 0

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

Re: Storing Text File into second Assortment of Chars

Posted 19 October 2010 - 05:12 PM

Thanks for the response macosxnerd. I appreciate it.
I know how it would agree those examples, simply I'yard non sure how I would go about doing it for a text file. The higher up code that I posted doesn't allow me to employ the toCharArray method. It says cannot conform char to type char.


#7 macosxnerd101 User is offline

Reputation: 12800

  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 05:xiv PM

You demand to initialize your second array. Also, notice how I assign c[0] an assortment, not c[0][0] an array. Remember that 2D arrays are an assortment of arrys. So with have one big array, with each element containing a char[].


#viii sport10 User is offline

  • D.I.C Caput

Reputation: 0

  • View blog
  • Posts: 181
  • Joined: 21-September x

Re: Storing Text File into 2D Assortment of Chars

Posted 19 October 2010 - 06:54 PM

I yet can't get this matter 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(Cord[] args) { 		Driver d = new Commuter(); 	} 	public Driver(){ 		loadMap(mapFile); 		Arrangement.out.println(map[0][i]); 	}   	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] = browse.nextLine().toCharArray(); 			} 		}catch(IOException e){ 			System.out.println("INVALID FILE"); 		} 	} }                

Is this not reading information technology right, considering I get a NoSuchElement Error when I compile it. I believe the code scans each line and then the n integer keeps runway 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 and then put into the second element of the 2nd array. What am I doing wrong?


#9 sport10 User is offline

  • D.I.C Head

Reputation: 0

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

Re: Storing Text File into 2D Assortment of Chars

Posted xix Oct 2010 - 07:25 PM

Am I even shut on this or am I doing it completely wrong?


#10 g00se User is offline

Reputation: 3744

  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Storing Text File into 2D Assortment of Chars

Posted 20 Oct 2010 - 12:10 AM

You didn't initialize the assortment. Y'all demand something more than like

                  public static char[][] loadMap(File f) throws IOException {         Scanner s = null;         char[][] result = new char[64][];          try {             south = new Scanner(f);              int i = 0;              while (s.hasNextLine()) {                 result[i++] = s.nextLine().toCharArray();             }              s.close();             effect = (char[][]) CompactArray.compact(event);             // Above method here: http://technojeeves.com/joomla/index.php/gratis/66-compact-array-in-java             // DEBUG - print it             System.out.println(Arrays.deepToString(result));              render result;         } finally {             try {                 s.close();             } grab (Exception e) { /* ignore */             }         }     }                

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


  • Java

robersonhilen1988.blogspot.com

Source: https://www.dreamincode.net/forums/topic/195775-storing-text-file-into-2d-array-of-chars/

0 Response to "How to Read a Text File of Char to an 2d Array in Java"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel