• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

.txt file to char array

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn’t realise you had to get rid of the spaces.
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic