File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 3160
    
  10
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: 3160
    
  10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Filling an Array.
 
Similar Threads
Reading characters from a file into an array
Large blocks of white space while outputting to a file?
Comparing chars at positions in an array
.txt file to char array
Trying to fill a 2D array from a file