Results 1 to 5 of 5
  1. #1
    Snackers is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Question need help with if statement in loop

    Apparently I don't understand conditional statements. I have a loop, and in the first iteration I need something to happen. The statement in my if (i==0) never executes. Basically I want to create a new ImageStack when the first image in the loop is loaded. Can some one tell me what I am doing wrong here?

    Java Code:
    		for (int i=0; i<files.length; i++) {
    			ImagePlus img = opener.openImage(path, files[i].getName());
    			if (i==0) {
                    ImageStack stack = new ImageStack(img.getWidth(), img.getHeight(), 2);
    			}
    
    			if (img!=null) {
    				img.show();
                    stack.addSlice(files[i].getName(),img.getProcessor().getPixels());
                    }
    		}

    Edited: I guess I should add my error message when I compile:

    C:\Program Files (x86)\ImageJ\plugins\Gallery_File_Opener.java:61: cannot find symbol
    symbol : variable stack
    location: class Gallery_File_Opener
    stack.addSlice(files[i].getName(),img.getProcessor().getPixels());
    ^
    1 error
    Last edited by Snackers; 01-11-2013 at 04:55 PM. Reason: add more info

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: need help with if statement in loop

    The variable 'stack' is local to that first if-statement body; define it before line #3 and it still exists in the second if-statement.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    viren2727in is offline Member
    Join Date
    Jan 2013
    Location
    INDIA
    Posts
    18
    Rep Power
    0

    Default Re: need help with if statement in loop

    Java Code:
    ImageStack stack = null;
    for (int i=0; i<files.length; i++) {
        ImagePlus img = opener.openImage(path, files[i].getName());
        if (i==0) {
            stack = new ImageStack(img.getWidth(), img.getHeight(), 2);
        }
     
        if (img!=null) {
            img.show();
            stack.addSlice(files[i].getName(),img.getProcessor().getPixels());
            }
    }
    ....
    stack = null; // In Last

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: need help with if statement in loop

    Setting a (non existent) variable to null doesn't help one bit.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Snackers is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: need help with if statement in loop

    Quote Originally Posted by JosAH View Post
    The variable 'stack' is local to that first if-statement body; define it before line #3 and it still exists in the second if-statement.

    kind regards,

    Jos
    Thanks that worked!

Similar Threads

  1. repeat if statement in loop
    By eng_hyzoom in forum New To Java
    Replies: 5
    Last Post: 03-05-2012, 12:34 PM
  2. If Else Statement in While Loop
    By rockintyler in forum New To Java
    Replies: 3
    Last Post: 02-23-2012, 11:33 PM
  3. Help with loop statement
    By arvind1508 in forum New To Java
    Replies: 2
    Last Post: 02-23-2011, 04:39 PM
  4. Need help with a loop statement
    By sunshine39 in forum New To Java
    Replies: 7
    Last Post: 11-03-2008, 03:42 AM
  5. Least To Greates[ if statement and for loop]
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-08-2008, 06:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •