import java.util.Scanner; import java.io.*; public class P1 { static final int ROW = 1000; static final int COL = 667; public static void readImage(int[][][] startImage, String fileName) { Scanner inputF = new Scanner(fileName); int row = 0, col = 0; int line = 1; while (inputF.hasNext()) { if (line <= 4) { inputF.nextLine(); line++; } else { line += 3; if (col < COL) { startImage[row][col][0] = inputF.nextInt(); startImage[row][col][1] = inputF.nextInt(); startImage[row][col][2] = inputF.nextInt(); col++; } else { row++; col = 0; startImage[row][col][0] = inputF.nextInt(); startImage[row][col][1] = inputF.nextInt(); startImage[row][col][2] = inputF.nextInt(); col++; } } } inputF.close(); } public static void printImage(int[][][] startImage) { for (int row = 0; row < ROW; row++) { for (int col = 0; col < COL; col++) { for (int color = 0; color < 3; color++) { System.out.println(startImage[row][col][color]); } } } USING THIS SAME CODE,CONVERT THE ARRAYS THAT ARE BIENG DISPLAYED INTO FILES USING FILEWRITER public static int[][][] getAverage(int[][][] p1, int[][][] p2) { int[][][] avgImage = new int[ROW][COL][3]; for(int i=0; i<ROW; i++) { for(int j=0; j<COL; j++) { for(int k=0; k<3; k++) { avgImage[i][j][k] = p1[i][j][k] + p2[i][j][k]; } } } return avgImage; } public static void main(String[] args) throws IOException { int[][][] startImage = new int[ROW][COL][3]; readImage(startImage, "start.ppm"); printImage(startImage); int[][][] finalImage = new int[ROW][COL][3]; readImage(startImage, "final.ppm"); printImage(startImage); int[][][] middlearray = getAverage(startImage, finalImage); printImage(middlearray); int[][][] fourtharray = getAverage(startImage, middlearray); printImage(fourtharray); int[][][] fiftharray = getAverage(finalImage, middlearray); printImage(fiftharray); } }
**************************************************
Modify the printImage method as below: public static void printImage(int[][][] startImage) throws FileNotFoundException { PrintWriter writer = new PrintWriter(new File("output.txt")); for (int row = 0; row < ROW; row++) { for (int col = 0; col < COL; col++) { for (int color = 0; color < 3; color++) { System.out.println(startImage[row][col][color]); writer.println(startImage[row][col][color]); } } } writer.close(); System.out.println("Created output.txt in " + System.getProperty("user.dir")); }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Get Answers For Free
Most questions answered within 1 hours.