It's not a secret anymore!*
The moose likes Java in General and the fly likes Filling an Array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Filling an Array." Watch "Filling an Array." New topic
Author

Filling an Array.

Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
the following code gets me stuck in an infinite loop, not sure why.

while(input.hasNext())
{
for(int i = 0; i < maze.length; i++)
{
for(int x = 0; x < maze[i].length; x++)
{

String insert = input.nextLine();

maze[i][x] = insert.charAt(x);
}
}
}
What I'm trying to do is read the characters on the line and fill them in the maze[][] (of type char) array. Not sure why this code isn't working like it should be.
Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
I've also tried hasNextLine instead of just hasNext, still getting the same problem.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
What is input ? If it is a class that blocks on a call to nextLine() if there is no more input, then it may just be sitting there waiting.
You do realise you are doing several calls to nextLine() (one for each time thru the inner loop) for each call to hasNext() that you do, so it's possible you call nextLine() when hasNext() would have returned false.


Joanne
Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
Input is a scanner, this is what my entire constructor looks like.

public Maze(String fileName) {

//Scanner to read file

Scanner input = null;
try{
input = new Scanner(fileName);
}catch(RuntimeException e) {
System.err.println("Couldn't find the file");
System.exit(0);
}
//Set the size of the maze
while(input.hasNextInt())
{
size = input.nextInt();
}

//Set Limits on coordinates
Coordinates.setLimits(size);

//Set the maze[][] array equal to this size
maze = new char[size][size];

//Fill the Array with maze values

for(int i = 0; i < maze.length; i++)
{
for(int x = 0; x < maze[i].length; x++)
{
if(input.hasNextLine())
{
String insert = input.nextLine();

maze[i][x] = insert.charAt(x);
}
}
}



}
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
I've never used Scanner so this is just a guess, but the Javadoc for the nextXXX methods says This method may block while waiting for input.
I would be tempted to put some print statements in to your code to see if you can find where it is looping/blocking.
 
 
subject: Filling an Array.