0

i am trying to read number of image files and i want to compare them with each other. I have created an array object of size 5 and assigned 5 images to it. I tried to compare each images by passing array object to method. But it shows me an error. Can anyone help me?

File[] f= new File[5];

 f[1]= new File("p1.png");

 f[2]= new File("p2.png");

 f[3]= new File("p3.png");

 f[4]= new File("p4.png");

 f[5]=new file("p5.png");

 for(int i=1;i<5;i++)

{

 compare(f[i],f[i++]);

}

public void compare(File fi[1],File fi[2])
{


    BufferedImage image = ImageIO.read(fi[1]);
 int width = image.getWidth(null);

  int height = image.getHeight(null);

int[][] clr=  new int[width][height]; 

  BufferedImage images = ImageIO.read(fi[2]);

 int widthe = images.getWidth(null);

   int heighte = images.getHeight(null);

int[][] clre=  new int[widthe][heighte]; 
}
3
  • 2
    I recommend taking a step back and working through some fundamental Java tutorials. Commented Mar 16, 2013 at 9:54
  • each time you are passing same two object, replace 'compare(f[i],f[i++]);' with 'compare(f[i],f[i+1]);' Commented Mar 16, 2013 at 9:55
  • what is this.... !!!! int width = image.getWidth(null); int height = image.getHeight(null); Commented Mar 16, 2013 at 9:56

8 Answers 8

2

Several issues:

  1. This line

    public void compare(File fi[1],File fi[2])
    

    is a syntax error. It should be

    public void compare(File f1, File f2)
    

    (obviously the names of the args are up to you).

  2. When calling it, use

    compare(f[i], f[i + 1]);
    

    rather than i++.

  3. This line tries to write to an array slot that doesn't exist:

     f[5]=new file("p5.png");
    

    ...because your array is defined as new File[5], which means it has slots 0 through 4. You'll need to change all of your lines setting values in the slots to use 0 through 4 instead of 1 through 5.

  4. Your loop boundaries are wrong. They should be 0 and < f.length - 1 instead of 1 and < 5 here:

    for(int i = 0; i < f.length - 1; i++)
    

    There are two reasons for doing < f.length - 1 rather than < 5. First, because you use i + 1 in the body of the loop, you want to stop one slot early. Secondly, if you change the length of f later, you'd have to remember to update the 5 on the for loop as well, which is a maintenance problem waiting to happen. (Note that the loop is to i < f.length - 1 because you use i + 1 in the loop body. Normally a loop is 0 through < length, but not in this case.)

I stopped reading the code at that point, so can't guarantee there aren't other fundamental issues. I recommend working through some basic Java tutorials.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. When i did the 1st one as you said, i still get error in the line public void compare(File f1,File f2)
0

Arrays are indexed from 0, not 1. Instead of this:

File[] f= new File[5];
 f[1]= new File("p1.png");
 f[2]= new File("p2.png");
 f[3]= new File("p3.png");
 f[4]= new File("p4.png");
 f[5]=new file("p5.png");

Use this:

File[] f= new File[5];
 f[0]= new File("p1.png");
 f[1]= new File("p2.png");
 f[2]= new File("p3.png");
 f[3]= new File("p4.png");
 f[4]=new file("p5.png");

Comments

0

change your method signature to

public void compare(File fi1,File f2)

Comments

0

There are a few problems with your code.

First, your for loop should use the exit condition i < f.length-1

Second, your compare method does not need the array references:

public void compare(File f1, File f2) {
    ....
}

Third, indexes start at 0, not 1.

Comments

0

Change this line

public void compare(File fi[1],File fi[2]){

to

public void compare(File fi,File fi2) {

or

public void compare(File[] fi,File[] fi2) {

Comments

0

You mean compare each other?

for (int i = 0; i < f.length; i++)
    {
        for(int j=i+1;j<f.length;j++){
         compare(f[i], f[j]);
        }
    }

Comments

0

Array of size 5 starts from 0-4 but you are accessing index of 5

File[] f= new File[5];

 f[1]= new File("p1.png");

 f[2]= new File("p2.png");

 f[3]= new File("p3.png");

 f[4]= new File("p4.png");

 f[5]=new file("p5.png");  // This is wrong 

value should be from 0-4

And also

 for(int i=1;i<5;i++)
{
 compare(f[i],f[i++]);  

if i==4 then i++ will become, so you'll get ArrayIndexOutOfBoundsException

 }

I will don't mention rest of errors as Rest of the answers have already posted them

Comments

-1
    File[] f= new File[6];

     f[1]= new File("p1.png");

     f[2]= new File("p2.png");

     f[3]= new File("p3.png");

     f[4]= new File("p4.png");

     f[5]=new file("p5.png");

     for(int i=1;i<5;i++)  
    {   
     compare(f[i],f[i+1]);
    }

    public void compare(File f1,File f2)
    {
     BufferedImage image = ImageIO.read(f1);
     BufferedImage image = ImageIO.read(f2);
 // compare as you like

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.