1

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

1 Answer 1

2

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");
}
Sign up to request clarification or add additional context in comments.

3 Comments

lol that's correct I suppose but not exactly what I'm after. I want a URL with the appended numbers. System.out.println(PUT_URL + i); was just for my testing. Should I just create a new variable which I assign PUT_URL + i to or is there a better way?
Modified my answer accordingly
Perfect. Thanks for the help.

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.