diff --git a/GetPixels.java b/GetPixels.java index 5515db0edbbe4776e87f8d01f6c6aee0e8b0bd4d..029f86cddf5538cc988e3ba7dc36e756b52d6d0a 100644 --- a/GetPixels.java +++ b/GetPixels.java @@ -1,49 +1,73 @@ -import java.io.File; -import java.io.FileWriter; -import java.awt.Color; -import java.awt.image.BufferedImage; -import javax.imageio.ImageIO; -public class GetPixels { - public static void main(String args[])throws Exception { - FileWriter writer = new FileWriter("rgb_value.txt"); - //Reading the image - File file= new File("foswiki.png"); - BufferedImage img = ImageIO.read(file); +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +/** +* AcceptFile class that reads a text file and identifies whether it is in the +* following format: +* 1) First two lines must contain exactly one integer (representing width & length) +* 2) There must be width * height more lines after +* 3) Each of these lines must have exactly 3 integers with values within 0 to 100 inclusive +* @param String fileName +* @return error messages if criterias are not met +*/ - //Retrieving image width and height - int width = img.getWidth(); - int height = img.getHeight(); - writer.write("Image Width: " + width + "\n"); - writer.write("Image Height: " + height + "\n"); +public class AcceptFile { + public static void main(String[] args) { + String fileName = "wrongInput.txt"; // Replace with the name of text file to check + + // BufferedReader is used to parse through text file line by line + try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { + // Read and check the first two lines to get the width and height + String line = br.readLine(); + int width = Integer.parseInt(line.trim()); + line = br.readLine(); + int height = Integer.parseInt(line.trim()); + if (width <= 0 || height <= 0) { + System.out.println("Error: width and height must be positive integers."); + return; - int red_c, green_c, blue_c; - for (int y = 0; y < img.getHeight(); y++) { - for (int x = 0; x < img.getWidth(); x++) { - //Retrieving contents of a pixel - int pixel = img.getRGB(x,y); - //Creating a Color object from pixel value - Color color = new Color(pixel, true); - //Retrieving the R G B values scaled to 0 to 100 - int red = (int)((color.getRed() / 255.0) * 100); - int green = (int)((color.getGreen() / 255.0) * 100); - int blue = (int)((color.getBlue() / 255.0) * 100); + } + + // Check that there are width * height more lines in the file + int expectedNumLines = width * height; + int currentNumLines = 0; // Counter to keep track number of lines + while ((line = br.readLine()) != null) { + currentNumLines++; + } + if (expectedNumLines != currentNumLines) { + System.out.println("Error: Expected " + expectedNumLines + " lines, but found " + currentNumLines + " lines."); + return; + } + + // Check that each of the following lines has exactly 3 integers between 0 and 100 + for (int i = 0; i < 2 + expectedNumLines; i++) { // We skip the first two lines + br.readLine(); + } + while ((line = br.readLine()) != null) { + String[] tokens = line.split("\\s+"); + if (tokens.length != 3) { + System.out.println("Error: Line \"" + line + "\" does not contain exactly 3 integers."); + return; + } + for (String token : tokens) { + int value = Integer.parseInt(token); + if (value < 0 || value > 100) { + System.out.println("Error: Value " + value + " is not between 0 and 100."); + return; + } + } + } - //int red = color.getRed(); - //int green = color.getGreen(); - //int blue = color.getBlue(); - - writer.append(red+":"); - writer.append(green+":"); - writer.append(blue+""); - writer.append("\n"); - writer.flush(); - } - } - - - - writer.close(); - System.out.println("RGB values at each pixel are stored in the specified file"); - } + br.close(); + + // When text file meets all the requirements + System.out.println("The file \"" + fileName + "\" meets all the requirements."); + } catch (IOException e) { + System.out.println("Error reading file \"" + fileName + "\": " + e.getMessage()); + } catch (NumberFormatException e) { + System.out.println("Error parsing integer \"" + "\": " + e.getMessage()); + } + } } +