I have a String that contains four different locations, separated by a comma. I am splitting the long String into four parts and assigning the locations to location variables. At the moment this is fairly simple (see below).
int noOfLocations = counter + 1;
//Splits the long input location string into the four locations.
String[] locations = inputLocation.split(",");
String location1 = locations[0];
String location2 = locations[1];
String location3 = locations[2];
String location4 = locations[3];
However, I would like to be able to input any amount of locations and assign a variable for each using a for loop.
I feel like this is a fairly simple thing to do, but I just can't seem to code this correctly. I have a counter which counts the number of commas and adds one to that number to find the number of locations.
Basically I'd like something that says:
String[] locations = inputLocation.split(",");
for i in range noOfLocations {
String location(i) = locations[i];
}
What is the correct syntax to write something like this?
locations[i]already is the variable you're looking for.