0

I have a string array a[] that contains several urls that I want to pass into my HttpGet code below

        for (int i = 0; i < size; i++);{
        try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()){
                HttpGet httpget = new HttpGet(a[i]);

                /*WRITE TO FILE*/
                /*try (CloseableHttpResponse response = httpclient.execute(new HttpGet(a[i]))){
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        try (FileOutputStream outstream = new FileOutputStream(AgentRequirements)) {
                        entity.writeTo(outstream);
                        }
                    }
                }*/

                /*SHOW EXECUTION*/
                System.out.println("Executing request " + httpget.getRequestLine());
                try (CloseableHttpResponse response = httpclient.execute(httpget)) {
                    System.out.println("----------------------------------------");
                    System.out.println(response.getStatusLine());
                    System.out.println(EntityUtils.toString(response.getEntity()));
                }
            }
    }

I have a failed build because it says that system cannot find symbol 'i'. Can anyone help please?

5 Answers 5

1

What you have here :

for (int i = 0; i < size; i++);{
...
}

is first a loop with an empty statement

for (int i = 0; i < size; i++);

followed by a block statement (that's as nothing to do with the loop)

{
...
}

The problem comes from your loop that iterate and do nothing, same as

for (int i = 0; i < size; i++){
    ;
}
{
...
}

Just remove the ; that divide the loop from the block of statemnt. That way, the block will be the one execute for each loop.

for (int i = 0; i < size; i++){
    ...
}

FYI :

It is important to know that nothing prevent you to add block statement anywhere in a method.

So you can have

public static void main(String[] args){
    {
        int i = 0;
        System.out.println(i);
        {
            int j = 0;
            System.out.println(i);
            System.out.println(j);
        }
        System.out.println(j); // ERROR - j doesn't exist here
    }
    System.out.println(i); // ERROR - i doesn't exist here
}

The idea is that i only exist in that block, you can't use it outside of the block.

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

1 Comment

Your answer is nicer. It really explains the why and how. Also i like your information snippet at the end :)
0

remove the ; at the end of the for loop, because if it stays there, then the forloop doesn't loop, because the block is already closed. That's why i cannot be found outside of the block (your try-catch).

for (int i = 0; i < size; i++);{
    // your code
}

is the same as this:

for (int i = 0; i < size; i++){}
{
    // your code
}

Comments

0
for (int i = 0; i < size; i++);
                              ^

You have a loose semi colon that breaks your for loop.

Comments

0

Your forloop is not well formated:

for (int i = 0; i < 10; i++);{

remove the semi colon ";"

for (int i = 0; i < 10; i++){

Comments

0
 for (int i = 0; i < size; i++);

Redundant symbol ';' at the end. It closes the 'for loop' operator

Comments

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.