Okay, so I'm writing a program that takes a .bin file and reads it in. Basically, the .bin file starts with 2 ints that represents a size of a map, for example, 7 and 7 (these represent pixels on a map). After the 2 ints, the rest of the file contains a whole lot of doubles that represent different elevations. The two ints are read in just normally, and then the remaining doubles are all fed into a 2D array. That's not my problem. I'm able to (at least I think I am) to do all this just fine. This is all done under the constructor. I have another method getMaximum which finds the maximum of the 2D array. Here's the code I have so far for both the constructor and the method:
Name of the program is Field, and this is the constructor
NOTE: And by the way, elevation is a static 2D array defined as a static variable to be used throughout the program as I will need access to it later, as well as width and height.Code:public Field(String path) throws IOException {
if (path.substring(path.length()-4).equals(".bin")) {
File file = new File(path);
DataInputStream in = new DataInputStream(new FileInputStream(file));
width = in.readInt();
height = in.readInt();
while (true) {
try {
elevation = new double[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
try{
elevation[i][j] = in.readDouble();
} catch(EOFException eof){
break;
}
}
}
} catch (EOFException eof) {
break;
}
}
} else {
System.out.println("File Incorrect, try again");
}
}
Name of method is getMaximum and here is the code
Now, when I try to create an instance of Field and run getMaximum with that instance, the program just keeps looping and looping and never stops running. I'm using an example file we were given to read in the numbers. So basically, the program should get all the numbers from the file, save the first two ints as height and width, and the rest as doubles in a 2D array. I can't figure out why the program doesn't work.. Again, my problem is that the program just keeps running and doesn't actually print anything out..Code:public double getMaximum() {
if (elevation.length == 0 || elevation[0].length == 0) {
return 0;
}
double max = elevation[0][0];
for (int i = 0; i < elevation.length; i++) {
for (int j = 0; j < elevation[0].length; j++) {
if (elevation[i][j] > max) {
max = elevation[i][j];
}
}
}
return max;
}
EDIT: Okay, so after some experimenting, I can say that the problem is not in the getMaximum method, so it must be in the constructor.. I can't see to pinpoint it though, can someone look it over for me to find the error?
EDIT2: Okay, so I think I solved it. In the while loop, it says while(true), so I think that the loop never goes false, so it just keeps going and going. So what I did was to create a boolean variable and set it to false before the while loop, and after the loop should be done, I set it to false. That seems to be working, but I'm going to do some more testing on it.

