Question: Please fix this Java code for me. I am receiving an ArrayIndexOutofBoundsException on the line where it says buffer[y] = v; Not sure what I

Please fix this Java code for me. I am receiving an ArrayIndexOutofBoundsException on the line where it says "buffer[y] = v;" Not sure what I need to modify to make this run correctly!

import java.util.Scanner; import java.io.File; import java.io.PrintWriter;

public class PPM { public static void main ( String args[] ) { try { System.out.print ( " PPM Image Processor " ); Scanner keyboard = new Scanner ( System.in ); System.out.print ( " Enter input filename: " ); String inFile; inFile = keyboard.next(); File file = new File( inFile ); Scanner input = new Scanner ( file );

String first = input.next(); int columns = input.nextInt(); int row = input.nextInt(); int max = input.nextInt(); int x = ( columns * 3 ); int y = 0; int buffer[] = new int [x];

Scanner keyboardTwo = new Scanner ( System.in ); String outFile; System.out.print ( "Enter output filename: " ); outFile = keyboardTwo.next();

PrintWriter writer = new PrintWriter ( outFile ); writer.println ( first ); writer.print ( columns ); writer.printf ( " %d", row ); writer.printf ( " %d ", max ); while ( input.hasNext() ) { int v = input.nextInt(); buffer[y] = v; y++; }

negateRed ( buffer, x );

for ( int i = 0; i < buffer.length; i++ ) { writer.println ( buffer[i] ); } writer.close(); }

catch ( Exception ex ) { System.out.println ( "An error has occurred!" ); ex.printStackTrace(); } } /** * negateRed * * This method negates the red value in the image. All P3-format * PPM images have an image depth of 255, which are used in * the method's calculations. * * @param buffer an integer array containing the image buffer * @param x an integer w/ the number of columns in the image * @return NONE */ public static void negateRed ( int [] buffer, int x ) { for ( int i = 0; i < x; i++ ) { if ( i % 3 == 0 ) buffer[i] = 255 - buffer[i]; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!