Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to fill a 2D array from a file

 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to fill a two-dimensional array (char Maze[][]) by reading data from a file, and have something like this.

//Fill the Array with maze values
for(int i = 0; i < maze.length; i++)
{
for(int x = 0; x < maze[0].length; x++)
{
maze[i][x] = Scanner.next();
}
}
However, maze is of type char, and .next() can only be used on strings of course. I also get the classic "static context error" with this code. Not sure how to get around this. THanks.
 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Worked out the static context error, but not sure how to go about match up the char array with a string data type still. I've tried things like creating a dummy string array and setting it equal to the char array, but to no avail.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bit odd, but Scanner doesn't have a nextChar() method. Perhaps it should. Maybeyou could use next("."), where "." is a regular expression for one character, any character. You could also call scanner.useDelimiter("") - after that, next() will always return a String containing single character. Or maybe it would work better (and probably be more straightforward) for you to use nextLine() to read an entire line as a single String, and then loop through the individual chars in the String using charAt(). I don't know what the format of the file is like, but those seem like good things to try out, at least.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic