#Basic Help also
1 messages · Page 1 of 1 (latest)
uhhh
?
error message:
java.lang.IllegalArgumentException: Invalid PPM format
at ImageEditor.getPixelValues(ImageEditor.java:105)
at ImageEditorTest.testGetPixelValues(ImageEditorTest.java:313)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
error should be obvious enough
also delete your comments, they don't help
all
i know the illegalargumentsexception when trying to read line one
is asserting to true
always
please stop deleting your mrssages
OH
@Test
public void testGetPixelValues() {
int[][] validPixels = {{1,2,3}, {4,5,6}};
assertArrayEquals(validPixels,
ImageEditor.getPixelValues(new Scanner("P3 1 2 255 1 2 3 4 5 6")),
"Tests correct PPM file");
Exception exception = assertThrows(IllegalArgumentException.class,
() -> ImageEditor.getPixelValues(null), "ImageEditor.getPixelValues(null)");
assertEquals("Null file", exception.getMessage(),
"Testing ImageEditor.getPixelValues(null) " +
"- exception message");
assertNull(ImageEditor.getPixelValues(new Scanner("P2 1 2 255 1 2 3 4 5 6")),
"Tests ppm file with invalid type");
assertNull(ImageEditor.getPixelValues(new Scanner("P3 abc 2 255 1 2 3 4 5 6")),
"Tests ppm file with non-integer cols");
assertNull(ImageEditor.getPixelValues(new Scanner("P3 -5 2 255 1 2 3 4 5 6")),
"Tests ppm file with non-positive cols");
assertNull(ImageEditor.getPixelValues(new Scanner("P3 1 abc 255 1 2 3 4 5 6")),
"Tests ppm file with non-integer rows");
assertNull(ImageEditor.getPixelValues(new Scanner("P3 1 -3 255 1 2 3 4 5 6")),
"Tests ppm file with non-positive rows");
assertNull(ImageEditor.getPixelValues(new Scanner("P3 1 2 180 1 2 3 4 5 6")),
"Tests ppm file with invalid max value");
assertNull(ImageEditor.getPixelValues(new Scanner("P3 1 2 255 1 2 3 4")),
"Tests ppm file with too few values");
assertNull(ImageEditor.getPixelValues(new Scanner("P3 1 2 255 1 2 3 x 5 6")),
"Tests ppm file with invalid RGB noninteger value");
assertNull(ImageEditor.getPixelValues(new Scanner("P3 1 2 255 1 2 3 -4 5 6")),
"Tests ppm file with invalid RGB integer value");
}
public static int[][] getPixelValues(Scanner in) {
if (in == null) {
throw new IllegalArgumentException("Null file");
}
// Read the first line and check for "P3"
String format = in.nextLine().trim(); // Read the first line
if (!format.equals("P3")) {
throw new IllegalArgumentException("Invalid PPM format");
}
// Read the next lines for width, height, and max color
while (in.hasNextLine()) {
String line = in.nextLine().trim();
// Skip comments
if (line.startsWith("#")) {
continue; // Ignore comment lines
}
// Now we should have a line with width, height, and maxColor
Scanner lineScanner = new Scanner(line);
if (lineScanner.hasNextInt()) {
int width = lineScanner.nextInt();
if (lineScanner.hasNextInt()) {
int height = lineScanner.nextInt();
if (lineScanner.hasNextInt()) {
int maxColor = lineScanner.nextInt();
if (maxColor != 255) {
throw new IllegalArgumentException("Invalid max color value");
}
// Create a 2D array for pixels
int[][] pixels = new int[height][width * 3];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width * 3; j++) {
if (in.hasNextInt()) {
pixels[i][j] = in.nextInt();
} else {
throw new
llegalArgumentException("Invalid input file");
}
}
}
return pixels; // Successfully read and return the pixel array
}
}
}
}
// If we reach here, something went wrong
throw new IllegalArgumentException("Invalid input file structure");
}
in.nextLine().trim(); // Read the first line
This should be obvious enough that you are reading a line, no need to comment it, same for all your comments
and to answer your question
"P3 1 2 255 1 2 3 -4 5 6" != "P3"
@shadow blade
then it means that your code is wrong
yeah ðŸ˜
so you don't want to read a whole line
i know bro
let me show you how the file is supposed to be formatted
P3
1 1
255
45 128 220
I believe
that is test1.ppm that is given to us
but that's not how the test looks like
so either the test or the file is wrong
so report this
tho you can still fix it
just use next instead of nextline
it should work for both
java.lang.IllegalArgumentException: Invalid input file
at ImageEditor.getPixelValues(ImageEditor.java:136)
at ImageEditorTest.testGetPixelValues(ImageEditorTest.java:313)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
new error
java.lang.Error: Unresolved compilation problems:
lineScanner cannot be resolved
lineScanner cannot be resolved
lineScanner cannot be resolved
lineScanner cannot be resolved
lineScanner cannot be resolved
lineScanner cannot be resolved
at ImageEditor.getPixelValues(ImageEditor.java:118)
at ImageEditorTest.testGetPixelValues(ImageEditorTest.java:313)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
well yea you have to fix this now
uhhh
Please use:
Please use this format for posting code:
```java
// Example java program
int value = 5;
System.out.println(value);
```
Which results in:
// Example java program
int value = 5;
System.out.println(value);
For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.
And make sure what you use is declared, and available at the expected point.