#Basic Help also

1 messages · Page 1 of 1 (latest)

whole nymphBOT
#

<@&987246399047479336> please have a look, thanks.

shadow blade
#

uhhh

shadow fossil
#

?

shadow blade
#

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)

shadow fossil
#

also delete your comments, they don't help

shadow blade
#

yeah im just not sure why its happening though

#

what comments?

shadow fossil
#

all

shadow blade
#

i know the illegalargumentsexception when trying to read line one

#

is asserting to true

#

always

shadow fossil
#

please stop deleting your mrssages

shadow blade
#

???

#

isnt that what you asked

#

im so lost

#

sorry ive been up for like 4 days

shadow fossil
#

I asked to delete your comments

#

in your code

shadow blade
#

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");
}
shadow fossil
#

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

shadow blade
#

right im just also

#

not sure what to do

#

I cant change the test

shadow fossil
#

then it means that your code is wrong

shadow blade
#

yeah 😭

shadow fossil
#

so you don't want to read a whole line

shadow blade
#

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

shadow fossil
#

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

shadow blade
#

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

shadow fossil
#

Scanner lineScanner = new Scanner(line);

#

remove this

shadow blade
#

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)

shadow fossil
shadow blade
#

uhhh

pseudo ore
#

Please use:

whole nymphBOT
#

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.

pseudo ore
#

And make sure what you use is declared, and available at the expected point.