Question
A completed program: AreaFillT.class is provided for your testing. To testing this program, enter coordinates in the argument list input area. For example, 2 3
A completed program: AreaFillT.class is provided for your testing. To testing this program, enter coordinates in the argument list input area. For example, 2 3 means for the third row and fourth column. Then use data file area2.txt to test this program. Using the coordinates given, check the symbol of this coordinate and change it to the symbol *. Then, check all adjacent (top, bottom, left and right) coordinates to see if they have similar symbol. If yes, change all to the * symbol. area2.txt: 4 3 +++ @+@ @+@ @@@ For example: enter 2 3 *area.txt in the Cmd argument list then run AreaFillT.class, you should get the following results: AreaFillT.class: Results ..........00 ...0....0000 ...000000000 0000.....000 ............ ..#########. ..#...#####. ......#####. ...00000.... after filling ..........** ...*....**** ...********* ****.....*** ............ ..#########. ..#...#####. ......#####. ...00000....
______________________________________________________________________
Please use comments so I can understand your code.
This is what I have so far but I keep getting a FileNotFoundException:
import java.util.*; import java.io.*; public class AreaFill{ public static char[][] arr = null; private static String file = "area3.txt"; public static void main(String[] args) throws FileNotFoundException{ Scanner java=new Scanner(new File(args[0])); arr = readFile(file); StringTokenizer st=new StringTokenizer(java.nextLine()); int row=Integer.parseInt(st.nextToken()); int col=Integer.parseInt(st.nextToken()); char[][] arr=new char[row][col]; int index = 0; while(java.hasNextLine()){ arr[index++]=java.nextLine().toCharArray(); } fill(arr, row, col, arr[row][col]); print(arr); } public static void fill(char[][] arr, int r, int c, char sym){ arr[r][c] = '*'; //check its' up, down, left, right if((r-1) >= 0 && arr[r-1][c] == sym){ //up fill(arr, (r-1), c, sym); } if((r+1) < arr.length && arr[r+1][c] == sym){ fill(arr,(r+1), c, sym); //bottom } if((c-1) >= 0 && arr[r][c-1] == sym){ //left fill(arr, r, (c-1), sym); } if((c+1) < arr[r].length && arr[r][c+1] == sym){ fill(arr, r, (c+1), sym); //right } } public static char[][] readFile(String file) throws FileNotFoundException{ Scanner infile = null; while(infile == null){ try{ infile = new Scanner(new File(file)); }catch(FileNotFoundException e){} } int row = Integer.parseInt(infile.next()); int column = Integer.parseInt(infile.next()); char[][] array = new char[row][column]; for(int r = 0; r < row; r++){ String nextline = infile.next(); for(int c = 0; c < column; c++){ array[r][c] = nextline.charAt(c); } } infile.close(); return array; } public static void print(char[][] arr){ for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[0].length; j++){ System.out.print(" " + arr[i][j]); } System.out.println(); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started