Carey Brown wrote:When you want to return more than one piece of data, the common approach is to create a small class that can hold all the pieces you wish to return.
An alternative is to break your method into two methods, one which creates a list of N random number, and the other that takes this list and returns a JSON string.
And yet another way is to pass in an empty List<Integer> as a parameter and have the method add to the list as it goes and also return the JSON string.
Lisa Austin wrote:I would like to also like to have access to the requestNumber value(s) . I'm not sure how to do this since it generated inside this method.
Junilu Lacar wrote:
Lisa Austin wrote:I would like to also like to have access to the requestNumber value(s) . I'm not sure how to do this since it generated inside this method.
You are having difficulty because the creation/generation of the requestNumber is very tightly coupled to the code that generates the result String. You need to loosen that coupling. So, instead of creating it as you have on line 3, you need to be able to call on something to provide the value instead.
Stephan van Hulst wrote:In this particular case, it's probably best to generate the random numbers first, then get the data for each one, and then turn it all into JSON.
There are many problems with your code:
You are declaring variables in scopes where they're not needed. Always declare them in the deepest scope possible, and only at the time they're needed. Don't create random number generators on the fly. Pass them in a method call, or assign them to a field from a constructor. You're not using your string builder effectively. There's no point in using a string builder if you're gonna preform string concatenation anyway. Don't use special cases to avoid appending a separator character you the last case. Use String.join() instead. Build JSON using a JSON object model, not with string operations.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |