• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Trying to fill a 2D array from a file

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic