Basically, I have to implement a variety of methods to mess around with a two-dimensional character array for a class (display, recolour, and zoom). I've got everything to work except for one problem- the 'zoom' function is meant to display the selected character and all character directly around it, but displaying an empty space wherever there isn't anything to display. However I cannot figure out how to make it do this (currently it just displays an error message if I try and 'zoom' into any character around the edge of the array.
Code below, I gutted everything not relevant and left only the display (d) and zoom (z) functions. To be clear, the problem is not in entering the initial co-ordinates out of bounds there's actually another method to handle that that I removed, the problem is in 'zooming' into a space around the edge of the array, which will return an error message as it tries to display out of bounds spaces.
Code:
import java.util.Scanner;
public class CharMapFORHELP {
public static void zoom(char[][] map, int row, int col) {
//the problem method
System.out.print(map[row - 1][col - 1]);
System.out.print(map[row - 1][col]);
System.out.print(map[row - 1][col + 1]);
System.out.println();
System.out.print(map[row][col - 1]);
System.out.print(map[row][col]);
System.out.print(map[row][col + 1]);
System.out.println();
System.out.print(map[row + 1][col - 1]);
System.out.print(map[row + 1][col]);
System.out.print(map[row + 1][col + 1]);
System.out.println(); //adds the newline
}
/**
* Displays the given character-based map
* with a leading and trailing blank line.
*/
public static void displayMap(char[][] map) {
for (int row = 0; row < map.length; row++) {
for (int col = 0; col < map[row].length; col++) {
System.out.print(map[row][col] + " ");
}
System.out.println(); //adds the newline
}
}
// code to convert String array into map. Truthfully
// I don't 100% understand whats going on here, this
// was provided to us already written.
public static char[][] stringToArray(String str) {
char[][] result;
String[] rows;
rows = str.split("\n");
result = new char[rows.length][];
for (int r = 0; r < rows.length; r++) {
result[r] = rows[r].toCharArray();
}
return result;
}
public static void main(String[] args) {
//Commands
final char CMD_DISPLAY = 'd', CMD_ZOOM = 'z', CMD_QUIT = 'q';
Scanner sc = new Scanner(System.in);
char[][] map; // the character-based map
char command; // user's entered command
int row; // |
int col; // |- command parameters
char fill; // |
String strMap = "####################\n" +
"#### #####\n" +
"# ##\n" +
"## ### ##\n" +
"###### #######\n" +
"## ##\n" +
"#### ## ## ##\n" +
"###### ######### ##\n" +
"####################";
map = stringToArray(strMap);
System.out.println("Char Map");
System.out.println("========");
System.out.println();
System.out.println("Enter commands (? for help)." +
" There are no further prompts after this point.");
do {
//read the next single-character command
command = sc.next().charAt(0);
switch (command) {
case CMD_DISPLAY:
displayMap(map);
break;
case CMD_ZOOM:
row = sc.nextInt();
col = sc.nextInt();
zoom(map, row, col);
break;
case CMD_QUIT:
break;
}
} while (command != CMD_QUIT);
}
}
One way would be to check if the array index will be out of bounds, and then print a different character.
Wa can make this easy by creating a new method to check if the character is out of bounds:
Now we can modify the zoom method to use the
getValue(map, row, col)
method like this: