In: Computer Science
home / study / engineering / computer science / computer science questions and answers / Using JAVA The Following Code Is Able To Read Integers From A File That Is Called "start.ppm" ...
Your question has been answered
Let us know if you got a helpful answer. Rate this answer
Question: Using JAVA The following code is able to read integers from a file that is called "start.ppm" ont...
Using JAVA
The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage".
Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage".
The purpose of this will be to add both arrays and then get the average
Save the average onto a separte 3darray,lets say you call it "middlearray"
Then add startImage and middlearray and get the average then save it to another array called "fourtharray"
Then add finalImage and middle array and get the average then save it to another array called "fiftharray".
Print all 5 arrays
I can not submit data file because there are about 667,000 integers on each file and both files are the same size. The file is from an image that got converted onto a ppm file. I don't really see the purpose in adding the file to this question either way. The code below is already capable of reading from one file. I just need to code that will allow me to read from a different file (lets say we call that file "final.ppm". Please write an individual method for each file that is being read. Then add answer the questions from above.
I have already posted this question 2 times and its been answered by the same person and everytime i run the program it just prints only 0's. If this questinon gets answered by the same person again its OK.
import
java.util.Scanner;
import java.io.*;
public class P1
{
public static void main(String[] args) throws IOException
{
final int ROW=1000;
final int COL=667;
File file= new File("start.ppm");
Scanner inputF= new Scanner(file);
int[][][] startImage= new int[ROW][COL][3];
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();
for(row=0;row {
for(col=0;col {
for(int color=0;color<3;color++)
{
System.out.println(startImage[row][col][color]);
}
}
}
}
}
/***********************************P1.java***********************************/
import java.util.Scanner;
import java.io.*;
public class P1 {
public static void main(String[] args) throws IOException
{
final int ROW = 667;
final int COL = 1000;
File file = new
File("start.ppm");
File file2 = new
File("end.ppm");
Scanner inputF = new
Scanner(file);
Scanner inputF2 = new
Scanner(file2);
int[][][] startImage = new
int[ROW][COL][3];
int[][][] endImage = new
int[ROW][COL][3];
int[][][] middleArray = new
int[ROW][COL][3];
int[][][] fourthArray = new
int[ROW][COL][3];
int[][][] fifthArray = new
int[ROW][COL][3];
int row = 0, col = 0;
int line = 1;
while (inputF.hasNext()) {
if (line <=
4)
{
inputF.nextLine();
line++;
} else {
line++;
if (col < COL) {
startImage[row][col][0] =
Integer.parseInt(inputF.nextLine());
startImage[row][col][1] =
Integer.parseInt(inputF.nextLine());
startImage[row][col][2] =
Integer.parseInt(inputF.nextLine());
col++;
} else {
row++;
col = 0;
startImage[row][col][0] =
Integer.parseInt(inputF.nextLine());
startImage[row][col][1] =
Integer.parseInt(inputF.nextLine());
startImage[row][col][2] =
Integer.parseInt(inputF.nextLine());
col++;
}
}
}
inputF.close();
row = 0;
col = 0;
line = 1;
while (inputF2.hasNext()) {
if (line <=
4)
{
inputF2.nextLine();
line++;
} else {
line++;
if (col < COL) {
row = 0;
endImage[row][col][0] =
Integer.parseInt(inputF2.nextLine());
endImage[row][col][1] =
Integer.parseInt(inputF2.nextLine());
endImage[row][col][2] =
Integer.parseInt(inputF2.nextLine());
col++;
} else {
row++;
col = 0;
endImage[row][col][0] =
Integer.parseInt(inputF2.nextLine());
endImage[row][col][1] =
Integer.parseInt(inputF2.nextLine());
endImage[row][col][2] =
Integer.parseInt(inputF2.nextLine());
col++;
}
}
}
inputF2.close();
for (row = 0; row < ROW;
row++) {
for (col = 0;
col < COL; col++) {
for (int color = 0; color < 3; color++)
{
middleArray[row][col][color]
= (startImage[row][col][color] + endImage[row][col][color]) /
2;
}
}
}
for (row = 0; row < ROW; row++)
{
for (col = 0;
col < COL; col++) {
for (int color = 0; color < 3; color++)
{
fourthArray[row][col][color]
= (startImage[row][col][color] + middleArray[row][col][color]) /
2;
}
}
}
for (row = 0; row < ROW; row++)
{
for (col = 0;
col < COL; col++) {
for (int color = 0; color < 3; color++)
{
fifthArray[row][col][color] =
(endImage[row][col][color] + middleArray[row][col][color]) / 2;
}
}
}
System.out.println("Start Image
array: ");
for (row = 0; row < ROW; row++)
{
for (col = 0;
col < COL; col++) {
for (int color = 0; color < 3; color++)
{
System.out.println(startImage[row][col][color]);
}
}
}
System.out.println("End Image
array: ");
for (row = 0; row < ROW; row++)
{
for (col = 0;
col < COL; col++) {
for (int color = 0; color < 3; color++)
{
System.out.println(endImage[row][col][color]);
}
}
}
System.out.println("Middle array:
");
for (row = 0; row < ROW; row++)
{
for (col = 0;
col < COL; col++) {
for (int color = 0; color < 3; color++)
{
System.out.println(middleArray[row][col][color]);
}
}
}
System.out.println("Fourth Array:
");
for (row = 0; row < ROW; row++)
{
for (col = 0;
col < COL; col++) {
for (int color = 0; color < 3; color++)
{
System.out.println(fourthArray[row][col][color]);
}
}
}
System.out.println("Fifth array:
");
for (row = 0; row < ROW; row++)
{
for (col = 0;
col < COL; col++) {
for (int color = 0; color < 3; color++)
{
System.out.println(fifthArray[row][col][color]);
}
}
}
}
}
I have your ppm files but
I'm not able to upload the output
Please let me know if you have any problem or modify the answer, Thanks:)