Results 1 to 9 of 9
  1. #1
    jasonw749 is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default set int = to for loop var

    so, i need to set something to the current for loop iteration and i can find no way to do this effectively.

    for example
    Java Code:
    int j;
    
    for(int i = 0; i < 10; i++) {
    
         if(whatever) {
              j = i;
         }
    }
    that requires i to be final, and i need to use this.

    i myself am not new to java, ive been using it years, but ive never had to do this, and i really wouldnt feel right posting this in advanced help
    Last edited by jasonw749; 10-28-2012 at 05:55 PM.

  2. #2
    TheElder777 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: set int = to for loop var

    Try defining j outside the for loop.

  3. #3
    jasonw749 is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: set int = to for loop var

    oh, i shouldve done that anyway, in my code the variable is outside.

    it actually works if its defined inside, ill change the OP

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: set int = to for loop var

    The code in the OP should work... Unless the loop is in a class in a class and j is declared outside the inner class.

  5. #5
    jasonw749 is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: set int = to for loop var

    actually, looking at it, it is in an inner class, because its in a mouse listener, so i assume theres no way of doing it how i want to?

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: set int = to for loop var

    I generally make the whole class into a mouselistener by implementing the MouseListener interface (or ActionListener, or WindowListener, etc). It means a few more methods that do nothing at all, but at least you can reach all your variables without a problem.

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: set int = to for loop var

    If you absolutely want an inner class though, I guess one way would be to throw the main class into it via the constructor and using a set/get method for the variable you want to reach, or just access them directly.

  8. #8
    jasonw749 is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: set int = to for loop var

    yeah, i do need the inner class, because inside the for loop, i create panels, then place them and add things to them all based on the for loop iteration value. then i add a MouseListener to the panel, all from inside the for loop.

    i just thought id give you this more info and see if it helps more :)

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: set int = to for loop var

    No ideas beyond the ones I posted in the previous post, sorry.

Similar Threads

  1. Converting a for loop to a do-while loop
    By awesom in forum New To Java
    Replies: 1
    Last Post: 11-23-2011, 03:02 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 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
  •