runnerpaul
runnerpaul

Reputation: 7236

Append number to end of url string

I have the below for loop which I use to append a number to the end of a URL String:

for(int i = 0; i < 5; i++){
    PUT_URL = PUT_URL + i;
    System.out.println(PUT_URL);
    sendPUT();
    System.out.println("PUT Done");
}

Currently the url appears in the format:

myurl1
myurl12
myurl123
myurl1234    
myurl12345

What's the best way to amend this so the url appears as?

myurl1
myurl2
myurl3
myurl4
myurl5

Upvotes: 1

Views: 594

Answers (1)

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22244

With this line

PUT_URL = PUT_URL + i;

you are modifying PUT_URL, that I presume contains myurl, by appending i to it and then printing. Therefore in the next iteration PUT_URL will contain a number at the end and then you are appending the next number.

I would suggest creating a constant with the prefix of a url without the number at the end and then append a number to that to create PUT_URL:

String URL_PREFIX = "myurl";

for(int i = 0; i < 5; i++) {
    PUT_URL = URL_PREFIX + i + 1;
    System.out.println(PUT_URL);
    sendPUT();
    System.out.println("PUT Done");
}

Upvotes: 2

Related Questions