In floodFill
you seem to be counting the size of the spots. If you want only the number of holes, then you don't need that. In that case you can change the loop in the main
method like this:
int holes = 0;
for (int x = 1; x < newGrid.length; x++) {
for (int y = 1; y < newGrid[x].length; y++) {
if (newGrid[x][y] == '-') {
++holes;
floodFill(x, y);
}
}
}
System.out.println("The file had " + holes + " cancer spots in it");
Basically, when you find a -
, increment the count, and use the floodFill
method to fill the hole so it's not counted again. (I took the liberty to change the loop end conditions to use newGrid.length
and newGrid[x].length
to make your code more flexible).
Also, in the floodFill
method it's pointless to set values with +
in them to +
again. You could simplify the method like this:
public static void floodFill(int row, int col) {
if (newGrid[row][col].equals('-')) {
newGrid[row][col] = ' ';
floodFill(row - 1, col - 1);
floodFill(row - 1, col);
floodFill(row - 1, col + 1);
floodFill(row, col - 1);
floodFill(row, col + 1);
floodFill(row + 1, col - 1);
floodFill(row + 1, col);
floodFill(row + 1, col + 1);
}
}
(Unless I missed something and you're looking for something more than just the number of holes.)