Results 1 to 4 of 4
  1. #1
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Execute main args.length number of times

    I want the number of times main's code is executed to be dependent on the number of comman-line arguments passed to the program. The least troublesome way I can think of is to wrap all of the code in a big for loop..

    But is there a better way to do it?

    Thanks in advance
    Last edited by cherrychives; 10-05-2012 at 01:00 AM.

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

    Default Re: Execute main args.length number of times

    If main() works on one command line argument at a time you could move its code to a different method. Then main() would just call this other method within a loop.

  3. #3
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Execute main args.length number of times

    Thank you pbrock.

    In my particular program, the minimum length of args is 3, and the first two arguments are used every time the code is executed, so it would it still be more efficient to invoke a separate method and pass 3 parameters each time?

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

    Default Re: Execute main args.length number of times

    I wasn't thinking of efficiency so much as clarity. One task: one (documented) method.

    The other two argumenst could also be assigned to static variables if that makes sense.

Similar Threads

  1. Why can't we write main without String args[]
    By shailender in forum New To Java
    Replies: 6
    Last Post: 06-17-2013, 09:28 AM
  2. Calling args from main method
    By bogojevicka in forum New To Java
    Replies: 3
    Last Post: 01-16-2012, 09:34 PM
  3. Dice help. posting the number of times a number is rolled.
    By cookiejarvus in forum New To Java
    Replies: 13
    Last Post: 12-04-2011, 11:08 PM
  4. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 PM
  5. presse 1 menuitem execute 4 times the same JFrame
    By marcvb in forum New To Java
    Replies: 0
    Last Post: 12-27-2007, 09:48 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
  •