• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

.txt file to char array

 
Matt Wilde
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize for that Syntax error however i would still suggest an ArrayList over a basic array
 
Matt Wilde
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn’t realise you had to get rid of the spaces.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic