Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Creating a "repeator" method.

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating a "repeator" method.

    Hey.


    I've been trying to figure out how to do this for a few hours now. I have this sort of code:
    new InventoryBuilder().add("Item").empty(8).add("Item");

    Now, if I would want, say 8 times item:
    new InventoryBuilder().add("Item", 8).empty(8).add("Item", 8);

    The issue with this is that I end up with a crap ton of methods, considering it might have up to 3 arguments. I figured I could do something like this:
    new InventoryBuilder().repeat(8, new Repeater() { add("Item") }).empty(8);

    But I have no idea how would I implement this


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Creating a "repeator" method.

    I feel like I walked into the theater in the middle of a movie. Are we supposed to know what you're talking about? Could you give us a little more to work with? What's the objective? What's your definition of a repeater method and what it does?

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a "repeator" method.

    It's a inventory builder for a game. Its idea is to place items in a inventory. The inventory has 9 items per row, so in this case it would be:

    with this code:
    new InventoryBuilder().add("Item").empty(8).add("Item");
    Would "output":
    "Item" *empty* *empty* *empty* *empty* *empty* *empty* *empty* *empty* "Item"


    Now if I would want:

    "Item" "Item" "Item" "Item" "Item" "Item" "Item" "Item" "Item"

    I'd have to write
    new InventoryBuilder().add("Item", 9);
    (The second argument being how many times to repeat it)

    Now the issue is, it can take quite a few arguments, it ALWAYS takes the "Item" argument, but it might also take "name" "lore" and bunch more. Having to add that "repeat" argument to every single method would be a hell. I thought I could make a "Repeat" method, which would take two arguments: one as how many times to repeat it and the second being the "method" aka ".add" to repeat that many times.

    With PHP I could make:
    PHP Code:
    inventoryBuilder($item$name ""$lore ""$repeat 1
    But Java doesn't have anything like that as far as I know.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Creating a "repeator" method.

    Java has loops and collections that can be iterated each time through the loop to repeat similar tasks multiple times with varying parameters. I think it's just a change in mindset from how you used to do it, but I'm not sure.

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a "repeator" method.

    I figured out one solution after stopping for a minute and thinking. Having only two arguments. add(Item, ItemOptions). ItemOptions being a class which could be used as following:

    new InventoryBuilder("Item", new ItemOptions().name("item").lore("items lore").repeat(8));

Similar Threads

  1. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  2. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  3. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM
  4. Creating a java program to "Beat" Snake web app?
    By AkOndray in forum AWT / Java Swing
    Replies: 2
    Last Post: December 5th, 2010, 12:36 AM
  5. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM