I\'m working on a Java assignment that processes image files. I need to modify t
ID: 3865230 • Letter: I
Question
I'm working on a Java assignment that processes image files. I need to modify this while loop somehow so that after the condition (y < x) is no longer true it creates a new array and continues to shove data from the rest of the file into the next array. Is there any way to do this? Any help is appreciated.
import java.util.Scanner;
import java.io.File;
public class processor {
public static void main ( String args[] ) {
try {
System.out.print ( " PPM Image Processor " );
// Create a regular Scanner for keyboard input
Scanner keyboard = new Scanner ( System.in );
// Create a String variable and read the filename
System.out.print ( " Enter input filename: " );
String inFile;
inFile = keyboard.next();
// Create a File variable based on the inFile file name
File file = new File( inFile );
// Create a Scanner attached to the file instead of System.in
// Used for file input!!!
Scanner input = new Scanner ( file );
// Read the "magic number"!
String first = input.next();
// Read number of columns from the file
int column = input.nextInt();
// Read number of rows from the file
int row = input.nextInt();
// Read max value from file
int max = input.nextInt();
// Loop that reads the rest of the int data from the file
int x = ( column * 3 ); System.out.printf ( "%d columns and %d rows", column, row );
int y = 0;
int RGB[] = new int [x]; System.out.printf ( " Read %d numbers from file!", x );
while ( input.hasNext() && y < x ) {
int v = input.nextInt();
RGB[y] = v;
y++;
}
}
catch ( Exception ex ) {
System.out.println ( "An error has occurred!" );
ex.printStackTrace();
}
}
}
Explanation / Answer
Add new array
into RGBx []= new int[x].
Modify while loop as
while(input.hasNext())
{
if( y < x ) {
int v = input.nextInt();
RGB[y] = v;
y++;
}
else{
{
int v = input.nextInt();
RGBx[y] = v;
y++;
}
}