1

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?

2
  • 7
    Why? locations[i] already is the variable you're looking for. Commented Sep 29, 2015 at 9:16
  • 4
    Yes, you dont have to create new String variables, just use locations[i] Commented Sep 29, 2015 at 9:17

3 Answers 3

5

You want to create programatically variables (accessed by index) and of them you want arbitrarily many. That's exactly what you already do with your locations array. As you know you can access single variables of this array via index. locations[i].

All you need to do, is to call

String[] locations = inputLocation.split(",");

and you will get your list of variables (array). You also don't need any constructs like:

int noOfLocations = counter + 1;

because that is what you get by calling locations.length

Sign up to request clarification or add additional context in comments.

Comments

2

It's almost impossible to create variables dynamically. They need to be write manually in source code (unless you use some magical byte code manipulators ).

But having already an array in hand locations and creating a dynamic variables contains the same info won't make lot of sense.

    String[] locations = inputLocation.split(",");
    for (int i = 0; i < locations.length; i++) {
        System.out.println(locations[i]);
    }

Look at the above loop you need not to create and you can access them directly.

Comments

2

You already have everything in the array. There is no need for any other variables.

String[] locations = inputLocation.split(",");
System.out.println("Found " + locations.length + " locations");

// for example
System.out.println("3. location is " + location[2]);

In case you need a new array with the same contents, you can copy original array like this:

String[] locationsCopy = new String[locations.length];
System.arraycopy(locations, 0, locationsCopy, 0, locations.length);

2 Comments

Don't forget : Arrays.copyOf(T[] original, int newLength)
Yep, that is also an option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.