0

I am getting a String array back from a service (or actually a comma separated String which I'm splitting into a String array), and I want to extract those into separate Strings with meaningful names, e.g.:

String value = contents[0];
String name = contents[1];
String date = contents[2];
String status = contents[3];
...

Since the array is long and there are many such Strings to extract, I'm looking for the shortest way to do this, but couldn't find anything on SO/ the web. Thanks!

Edit

As an example, the input could be something like ["foo","bar","2017/04/01","A"], I want to extract those to meaningfully named Strings as described above, and then use those along the way, e.g. call function1(name, date) and then function2(name, value) etc. - I only want this translation (which needs to know which array index holds what data) to happen once and spare my code the need to know this mapping of array index to meaning everywhere. So I could have a translate function that does that and inserts the values into a map or something, but I thought that since these are all Strings there should be a way for me to initialize all of them at once.

Dan
  • 37
  • 8
  • from where you will get the meaningful names. I mean how you will map the values to variables – dasrohith Apr 28 '17 at 07:38
  • If you want to assign separate standalone variables as you are doing here, then what you already have might be as good as it gets. You might be able to convert the array into some sort of collection, if that would still satisfy your requirement. – Tim Biegeleisen Apr 28 '17 at 07:39
  • 1
    tell us example input and output – Anand Siddharth Apr 28 '17 at 07:39
  • the API defines the meaning of each value, therefore I know the first entry in the array stands for value, the 2nd stands for name, the 3rd stands for date, the 4th for status etc. And example input may be ["foo","bar","2017/04/01","A"....] which should be translated into value="foo",name="bar",date="2017/04/01", status="A" etc. – Dan Apr 28 '17 at 08:49
  • How many names are we talking about here (10, 100, 1,000?), and what do you intend to do with them once you've assigned them all to separate variables? – dimo414 Apr 28 '17 at 09:00
  • An array should have around 15 Strings in it – Dan Apr 28 '17 at 09:05
  • How do you know the service won't change the order/it will always be set. – Darren Forsythe Apr 30 '17 at 23:33
  • This is the contract, the Service is guaranteed not to change this order. Not the best interface possible, I know... – Dan May 01 '17 at 10:13
  • @Dani sorry about the Java/JS mixup :) – Tibrogargan May 01 '17 at 11:51
  • You can't create variables dynamically in Java. The best you can do is to create dynamic references to member variables. See [this](http://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java). – Tibrogargan May 01 '17 at 11:55

2 Answers2

1

Unless the input data contains field names or other metadata you will not get around a piece of code that associates names with indices, like in your code snippet above.

You could create a class for the kind of object encoded in the string array and supply a constructor or static factory method that initializes the object from a string array.

class WhateverItIs
{
   public static WhateverItIs fromStrings(String[] strings)
   {
       WhateverItIs instance = new WhateverItIs();
       instance.value = strings[0];
       instance.name = strings[1];
       // ...
       return instance;
   }

   private String value;
   private String name;
   // add getters
}

Clients can then access the properties of the created object.

Regarding the edit of your question: the functions you mentioned could then become methods of the class, possibly not needing any parameters any more because the data is in the instance variables.

JayK
  • 3,006
  • 1
  • 20
  • 26
  • Thanks, I thought about that, but it doesn't seem to be saving me anything, this requires even more lines of code than I currently have with just creating Strings – Dan Apr 28 '17 at 09:16
  • Only you know in how many places you will need that kind of record of strings. If it is only in one place of your code base, your existing solution is probably as good as it will get. You only need to create variables for the items you are I interested in, of course. If the input contained labels, you could create a Map from it. – JayK Apr 28 '17 at 09:19
0

Why do you want to use have multiple Strings from the array. It will unnecssarily increase the JVM memory. A better option is to have it in a list. having said that if the data is in the form of name and value, you can always use a map, where key could be the name and value as is.

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
  • I don't see how using a List would help me, I would still need to know the index of each entry in the list. I want to parse the input array once (so I only have to know the meaning of each index in the array there) and then have meaningful names I could use (e.g. if I only want to use the name and status in one place, then the name and value in another) – Dan Apr 28 '17 at 08:54
  • do you have a sequence, or, how can you make the system understand what does each value represent. If you have the logic we can implement – Saurabh Jhunjhunwala Apr 28 '17 at 09:00
  • The logic is as described in my edited question - I need to do stuff with these Strings, send them to other services that save them to the filesystem for example. I know how to implement the logic, I'm just looking to translate this input into Strings with meaningful names, and for the most efficient way to do that. – Dan Apr 28 '17 at 09:08