how to form a dynamic string array using java

Hi Experts,
how to form a dynamic string array from the below inputs

String countries="sg,sk,hk,ke" //this string having country names dynamically
countries max will be five and min will be one value separated with comma(,)

i have to form dynamic string array based on above countries
user can choose any country but max will be 5 countries min 1 country
country values are dynacmic

how to form dynamic string array

new String[] {"sheet one"+ "sg" , "sheet two" + "sk","sheet three" + "hk","sheet four" + "ke","sheet five" + "ug","summary"}
new String[] {"sheet one"+ "sg" , "sheet two" + "sk","sheet three" + "hk","sheet four" + "ke","summary"}
new String[] {"sheet one"+ "sg" , "sheet two" + "sk","sheet three" + "hk","summary"}
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Open in new window

will give you String[] dynamically
srikoteshAuthor Commented:
hi cehj,
I have to add sheet names as well
 if the user choosen 3 countries

sheetname2+country name

here sheet names I am adding based on no of countries choosen
at the end summary(constant) I have to add.

the only input I will get is country names.
sheet names,summary I have to add along with country names.
Well, you can iterate the array and do what you want with it
Jan LouwerensSoftware EngineerCommented:
      Map<Integer, String> numbers = new HashMap<>();
      numbers.put(1, "one");
      numbers.put(2, "two");
      numbers.put(3, "three");
      numbers.put(4, "four");
      numbers.put(5, "five");
      numbers.put(6, "six");
      numbers.put(7, "seven");
      numbers.put(8, "eight");
      numbers.put(9, "nine");
      numbers.put(10, "ten");
      String countries = "sg,sk,hk,ke";
      String[] countryNames = countries.split(",");
      String[] results = new String[countryNames.length + 1];
      for (int index = 0; index < countryNames.length; index++)
         results[index] = "sheet " + numbers.get(index + 1) + ": " + countryNames[index];
      results[countryNames.length] = "summary";

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.