Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By wsaryada
  • 1 Post By wsaryada

Thread: Is There a way to Only Execute A line Of Code once?

  1. #1
    AyooNisto is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Is There a way to Only Execute A line Of Code once?

    Is it possible to only execute a line of code once and skip over it every time it reads the code again?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Is There a way to Only Execute A line Of Code once?

    You didn't bother to reply to the two responses in the first thread you started. Why should we expect any better behavior this time round?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: Is There a way to Only Execute A line Of Code once?

    I would do so

    Java Code:
    boolean check  = false;
    
    public void onlyOnce(){
    if(check == false){
    //your code here
    }
    check = true;
    }
    so the first time the boolean will be false, and the code will be executed, and the other times the boolean will be true, and the code will be skipped ;)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Is There a way to Only Execute A line Of Code once?

    Quote Originally Posted by Chri View Post
    Java Code:
    if(check == false){
    That's bad code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: Is There a way to Only Execute A line Of Code once?

    Quote Originally Posted by DarrylBurke View Post
    That's bad code.

    db
    whoops, I always did so...
    your post would be more useful if you would write the good code, wouldn't it?
    Last edited by Chri; 12-15-2012 at 02:22 PM.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Is There a way to Only Execute A line Of Code once?

    You can simply write your code like:

    Java Code:
    if (!check) {
    ...
    }
    DarrylBurke likes this.

  7. #7
    AyooNisto is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Is There a way to Only Execute A line Of Code once?

    I honestly forgot that I had made that thread I apologize for not responding to it.

    Quote Originally Posted by DarrylBurke View Post
    You didn't bother to reply to the two responses in the first thread you started. Why should we expect any better behavior this time round?

    db

  8. #8
    AyooNisto is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Is There a way to Only Execute A line Of Code once?

    Thanks for the help guys but i should have been clearer in my original post, the problem that I am having is that within A method I need to create an ArrayList, the only problem is that when i call the method more then once it continually creates a new array list resetting the size of the array list to null each time, i need to be able to call the method without it clearing the array list so it can hold the values of the list each time when the method is called.

  9. #9
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Is There a way to Only Execute A line Of Code once?

    You can just convert the code above and use it in your scenario. You just need a condition when you will create the ArrayList and when not. For example check if the ArrayList is null or not. By knowing this condition you know whether you need to instantiate the ArrayList or not.
    pbrockway2 likes this.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Is There a way to Only Execute A line Of Code once?

    within A method I need to create an ArrayList, the only problem is that when i call the method more then once it continually creates a new array list
    I have trouble understanding this.

    You say you "need" (=="have freely decided") to create an array list, but then say that it is problematic that the method does this very thing.

    Perhaps you meant something like that you intend to create an array list under certain circumstances and not every time the method gets called, in which case that's precisely what you should do. In the discussion above these "certain circumstances" were illustrated with a boolean variable check, but anything your method can check will do: a common case is to check the value of the variable and only create an array list if it is null.

    If this is all a bit vague, consider posting a code example: something brief, but compilable and runnable. Describe both what it does and what you intend its behaviour to be.

  11. #11
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: Is There a way to Only Execute A line Of Code once?

    I think that if he surrounds the declaration of the arraylist with an if statement, any time he will try to use it outside the if statement he will get an error, cause the program don't know if the arraylist will exist or not (I'm not totally sure of what I'm saying, but trying I obtained this result)
    EDIT: can't you simply create the arraylist outside the method?
    Last edited by Chri; 12-16-2012 at 01:06 PM.

  12. #12
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Is There a way to Only Execute A line Of Code once?

    The ArrayList can be declared as an instance variable of the class and the method will only contains the instantiation process of the ArrayList.

Similar Threads

  1. Code Won't Execute, Help Apreciated
    By danielinthebed in forum Networking
    Replies: 2
    Last Post: 01-01-2012, 12:00 AM
  2. how to execute a code after the endless loops?
    By Jhovarie in forum Threads and Synchronization
    Replies: 4
    Last Post: 07-24-2011, 04:21 AM
  3. Constructors pls explan the code line by line in comments
    By vibaviattigala in forum New To Java
    Replies: 1
    Last Post: 02-19-2011, 04:03 AM
  4. Unable to execute my code can u plz help me out
    By Gayathri12 in forum New To Java
    Replies: 6
    Last Post: 07-21-2010, 08:28 AM
  5. Execute code from ActionListener
    By Viola in forum New To Java
    Replies: 3
    Last Post: 05-29-2010, 05:10 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
  •