Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes .txt file to char array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ".txt file to char array" Watch ".txt file to char array" New topic
Author

.txt file to char array

Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
HI!
I have a "maze.txt" file as such:



the first two numbers represent the rows and columns (used for setting up the maze with the file reader).
The # sign represents a wall
The . represents a path
The S is start and E is end.
How would I read in this file and represent it as a 5x4 array of characters?
(will be solving this in the near future with recursive coding)
Help appreciated. Thanks!
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

What have you tried so far? Post some code showing us where you're stuck and we can help you with the specifics.
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34

now what i need to do is draw the maze some how. in the console of course. so would i scan the file for nextChar, nextChar, nextChar and just add those in to a character array then step through and print char, print char, print char. For each row in the maze?
Carl Ross
Greenhorn

Joined: Apr 09, 2012
Posts: 6
You Should Use a "nested" ArrayList

you then use either the scanner or buffer reader class (I would suggest the bufferreader) to get each char and add it to the nested arraylist
the outer can handle the rows and the inner the collums
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Carl, note that you can't use primitives as generic type arguments. You can also use simple List variables, because you shouldn't care about ArrayList functionality. Keep in mind that by convention Java variable names start with a lower case. So that would be:

A char[][] is probably simpler for this job though.
Carl Ross
Greenhorn

Joined: Apr 09, 2012
Posts: 6
I apologize for that Syntax error however i would still suggest an ArrayList over a basic array
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
Awesome! I used a char[][] and the BufferedReader and can draw the maze. Now, I want to solve it recursively. I will have the recursive code in another method called "mazeTraversal" and will call that method inside "openMaze"
I just want to know how I would make the program start at the "S" in the text file and continue from there to scan for possible solutions?

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Carl Ross wrote:I apologize for that Syntax error however i would still suggest an ArrayList over a basic array


Why?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Hi Matt,

Let's say you had a function that "magically" finds the path to the exit, given a maze and a current position.
Now, you could write a function that checks if you could find an exit from north, east, south and west of the current position, provided that the way back (the current position) was walled off.

If such a path exists from that direction, you can add that direction to the start of the path, and return the list of directions. Show us how you would implement such a function if you had the magic findExit() method.
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34


Maybe? and then do that for all directions like x, y-1 or x-1,y or x,y+1?

But if i had the magic one then i would just do

Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
In order to put myself into the maze I need to turn the maze into an actual grid and thats what I am trying to do here but I get outofboundsexception on my last "for" loop

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

All right, here's a skeleton for you. Take a good look at how this code transforms a maze text file into an actual Maze object. Such a Maze object is much easier to manipulate, because you can define methods that perform small operations on it.

Now see if you can implement the findExit() method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Matt Wilde wrote: . . . How would I read in this file and represent it as a 5x4 array of characters? . . !
There’s no such thing as a 5×4 array. What you had was a 5-element array whose elements are 4-element arrays.

Has nobody suggested a String[] or List<String> and the String#toCharArray() method?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

I don't really think that method is appropriate, since the lines seem to contain spaces, and you just want the tokens.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Didn’t realise you had to get rid of the spaces.
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

As an aside, the only proven way to get out of a maze is to keep checking to your right.

You would have to maintain a "direction" variable and adjust as you "walk" recursively.

Try it on a piece of paper!

Pat.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

The only proven way sounds a bit strong.

Sadly, it also doesn't work in general; for example, if your maze contains circular paths. Not unless you also have a way of marking them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: .txt file to char array