• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing a variable amount of data to a function  RSS feed

 
Doug Matthews
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!  I'm new to Java.  I'm running into a problem and I can't figure out the right way to solve it.  I wrote a plugin for a game I play and it has a few strings it outputs when it does stuff.  I wanted to make it more customizable so I made the strings configurable by the user.  So now I made a function to get the string from the config, do a replace on various tokens with data then output the string.  Originally it was just two tokens - player name and item name.  I did it like:


Worked great.  Problem is, I keep adding tokens.  It is way too unmanageable to have them all laid out in the function declaration.  I decided to try a hashmap method:


This is how I called it:


Worked great as well, but has readability problems and had warnings on every line: "The serializable class does not declare a static final serialVersionUID".  That lead me read this article which made me realize that's not a good way to do it.  I was trying to save clutter, because having to do it this way every time I want to display a string:


Will add so much bulk.  Though I suppose it's the same bulk actually, just vertical instead of horizontal heh.  I dunno why that bothers me, but it does.

Sooooooooo, if you are still reading after all this lol, do you know a better way to do this?  Could I make a custom object (class?) with parameters I can set then pass that object?  The back of my mind is saying that may be the "proper" way to go, but I'm lost on how to do it or even how to Google an example because I'm not sure the right terminology. 

Sorry for being such a noob, thanks in advance
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Java variable-length arguments list. The receiving parameter is an array. You'll have to make a check that it has an even number of elements in it to make sure the last item is a value, not a key. That is, you treat the array of String as one that alternates between {key, value, key, value, key, value, ...}
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch!
 
Doug Matthews
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You can use Java variable-length arguments list. The receiving parameter is an array. You'll have to make a check that it has an even number of elements in it to make sure the last item is a value, not a key. That is, you treat the array of String as one that alternates between {key, value, key, value, key, value, ...}


Cool, thanks a lot.  One question though... from what I see Googling it, it acts like an array.  Is there any way to pass it like "void dosomething(String...data)" and reference data by a string index?  Because lets say I have 5 tokens, A-E.  If I need to call the function and only pass A, C and E, how will I know it's A, C and E within that function if it's just accessing like data[0], data[1] and data[2]?  I suppose I could set each token to a fixed position where A=0, E=4 but then wouldn't I have to call it each time like "dosomething("some", "", "test", "", "data")"?


Junilu Lacar wrote:And Welcome to the Ranch!


Thanks! 
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how you went from your original example to this latest usage scenario but it's quite a leap. The original scenario of attaching implicit semantics of alternating key-value pairs to a varargs parameter is close enough to being a bad idea as it is. I just suggested it because you asked if there was *any* way. The usage that you described in your last response, however, is totally wacky and I would question why you need to assign specific fixed positions to a variable length list of arguments. In that case, maybe you are better off using a single object that encapsulates all the data points that you need to pass along.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doug Matthews wrote:I wanted to make it more customizable so I made the strings configurable by the user...

I'm not sure exactly what you mean by this - perhaps you could provide a bit more information - but what you describe sounds a bit like a Properties object to me.

Rather than having each Player supply a pile of parameters, they keep them in a local file, made up of
  key=value
lines, which you load into a Properties object at runtime. Then all they would have to do is supply the path to their items file.

HIH

Winston
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try Googling java builder pattern and see if that is something you could use.
 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doug Matthews wrote:Hello!  I'm new to Java. . . . thanks in advance


Hi, Doug,

Please use the varargs feature of Java with the following syntax:

public void doSomething(String... var)

The compiler will transform String... var into String[] var, and you are good to go.

With best regards,

Anton.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't quote the whole of an old post; it simply makes the thread longer without adding more information.
Somebody has already explained why varargs is not necessarily the best solution to this problem.
 
Doug Matthews
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I'm just approaching it wrong.  I want to be able to replace tokens in a string with piece of data.  A configured string for giving a player an item may be "$pl on $wr was given $qt of $it".  The system would then replace $pl with the player, $wr with world, $qt with quantity and $it with the item name. 

It would work if I did something like this:

..but that's a lot of duplication.  So I wanted to move processing to a function then call it, but my conundrum is how do I efficiently pass the token data to the function?  This would work:


but it's also pretty cumbersome.  I guess what I ultimately am asking is what's the best way to do this?


Junilu Lacar wrote:The usage that you described in your last response, however, is totally wacky and I would question why you need to assign specific fixed positions to a variable length list of arguments.

No doubt.  I wasn't illustrating what I -want- to do, rather what I was trying to avoid. 


Winston Gutkowski wrote:Rather than having each Player supply a pile of parameters, they keep them in a local file, made up of
  key=value
lines, which you load into a Properties object at runtime. Then all they would have to do is supply the path to their items file.

Well, these data are dynamic so I don't think using a file will really work well.


Knute Snortum wrote:Try Googling java builder pattern and see if that is something you could use.

I'll check it out, thanks.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
processTokens() can be improved thusly:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!