How to Read in Text File and Save in Char 2d Array in Java

ix Replies - 22237 Views - Terminal Post: twenty October 2010 - 12:ten AM Charge per unit 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 consignment for form I am suppose to take a text file and make a maze using # = for walls,
blank spaces = movable spaces, a = starting betoken, * = ending point.
How would I put the already made text file into a 2D array of chars?
Example:

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

This would accept to be put into a second array of Chars, merely 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 case image isn't showing up how it is suppose to.

This postal service has been edited past sport10: 19 Oct 2010 - 01:36 PM


Is This A Practiced Question/Topic? 0

  • +

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

#ii g00se User is offline

Reputation: 3744

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

Re: Storing Text File into 2nd Array of Chars

Posted 19 October 2010 - 02:31 PM

Use a Scanner and s.nextLine().toCharArray


#3 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 - 02:32 PM

You could read each line in every bit a String, and shop them in a Listing<String>. And so create a new char[][] with the start dimension equal to the size() of the List, and the second dimension left blank. So: char[][] c = new char[myList.size()][];. Yous can then utilise the String toCharArray() method to convert each String to a char[], and assign each char[] to c[someIndex].


#4 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 xix October 2010 - 05:03 PM

Hither's what I got now:

import java.io.*; import java.util.*;  public class Driver { 	File mapFile = new File("map.txt"); 	 	public static void main(Cord[] args) { 		Commuter 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; browse.hasNextLine();i++){ 				line = scan.nextLine(); 				map[i][]=line.toCharArray(); 				Organisation.out.println(line.length()); 			} 		} catch(IOException e){ 			Organization.out.println("INVALID FILE"); 		} 	} }                

I'1000 not too familiar on the toCharArray method. I looked on google for some examples, simply there are very few toCharArray examples for 2d arrays. Whatsoever Suggestion?


#five macosxnerd101 User is offline

Reputation: 12800

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

Re: Storing Text File into 2nd Array of Chars

Posted 19 October 2010 - 05:07 PM

Hither is a basic example:

char[][] c = new char[two][1]; c[0] = "12345".toCharArray(); c[ane] = "23456".toCharArray();                

Basically, c[0] holds the array {'ane','2','3','four','v'}, and c[one] holds the array {'ii','3','4','five','6'}.


#6 sport10 User is offline

  • D.I.C Caput

Reputation: 0

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

Re: Storing Text File into second Array of Chars

Posted 19 October 2010 - 05:12 PM

Thanks for the response macosxnerd. I capeesh it.
I know how it would hold those examples, just I'grand not sure how I would become about doing it for a text file. The above code that I posted doesn't allow me to apply the toCharArray method. It says cannot arrange char to blazon 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:14 PM

Y'all demand to initialize your 2D array. Likewise, detect how I assign c[0] an array, non c[0][0] an array. Remember that 2D arrays are an array of arrys. And then with accept 1 big array, with each chemical element containing a char[].


#viii sport10 User is offline

  • D.I.C Head

Reputation: 0

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

Re: Storing Text File into 2D Array of Chars

Posted 19 October 2010 - 06:54 PM

I still can't go this thing to store what I need it to. Hither is what I accept now:

import coffee.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) { 		Commuter 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(); 				northward++; 			} 			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, considering I get a NoSuchElement Fault when I compile it. I believe the code scans each line and so the due north integer keeps runway of each line, then assigns that to the 1st array dimension in the char[][] map array. Then information technology takes the length and takes the strings of the lines and stores them as chars, which is and then put into the 2d element of the 2nd array. What am I doing wrong?


#9 sport10 User is offline

  • D.I.C Caput

Reputation: 0

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

Re: Storing Text File into 2D Array of Chars

Posted 19 Oct 2010 - 07:25 PM

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


#x g00se User is offline

Reputation: 3744

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

Re: Storing Text File into 2D Array of Chars

Posted 20 October 2010 - 12:10 AM

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

                  public static char[][] loadMap(File f) throws IOException {         Scanner due south = goose egg;         char[][] result = new char[64][];          try {             due south = new Scanner(f);              int i = 0;              while (southward.hasNextLine()) {                 result[i++] = s.nextLine().toCharArray();             }              s.close();             upshot = (char[][]) CompactArray.meaty(consequence);             // Above method here: http://technojeeves.com/joomla/alphabetize.php/gratuitous/66-compact-array-in-java             // DEBUG - print it             Organisation.out.println(Arrays.deepToString(issue));              return result;         } finally {             try {                 s.shut();             } grab (Exception e) { /* ignore */             }         }     }                

This postal service has been edited by g00se: 20 October 2010 - 12:12 AM
Reason for edit:: Formatting buggy (all the same)


  • Coffee

vasquezpeturber.blogspot.com

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

0 Response to "How to Read in Text File and Save in Char 2d Array in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel