3

I am running a web crawler for my own use. I downloaded one and want to provide it with seeds. I want to provide it with around 50 seeds. So I created an array of strings. I want to provide the crawler with 1 seed each time it goes through my for loop. My code is :

String[ ] temp = new String[ ] {"http://www.random.org/","http://www.wikipedia.org/", "http://www.jlworld.com/","http://www.frys.com/"};

String[ ] urls = new String[temp.length];

  for (int i = 0; i <=temp.length; i++)       
     {          
        urls[i] = temp[i];      
     }

The crawler needs a string assigned to urls. So like :

String[ ] urls = new String[1];

urls[0] = "http://www.google.com/";

So it works like that. I get an outofbounds exception though for my code. What I want to do is provide the crawler with 1 of the seeds each time it goes through the for loop. Any help on this would be appreciated!

1

6 Answers 6

5
for (int i = 0; i <=temp.length; i++)

Should be:

for (int i = 0; i <temp.length; i++)
Sign up to request clarification or add additional context in comments.

Comments

2
for(int i = 0; i < temp.length; i++)

Calling .length on an array returns the size of the array, but array indices are 0-based. For your loop, you were correct in starting with i = 0, but you only want to go to i = (temp.length - 1) or you will get an index out of bounds exception. Comparing i < temp.length instead of i <= temp.length accounts for this shift in index basing.

1 Comment

There's no such thing as an "1-based size". The size is the size. If there are 10 elements in the array then the size is 10. Simple.
1

Change the line

for (int i = 0; i <=temp.length; i++)

to

for (int i = 0; i < temp.length; i++)

You were looping one too many times

Comments

0
for (int i = 0; i < temp.length; i++) {          
    String url = temp[i];      

    ... handle url...
}

Comments

0

Why not have a Map of [URL, seed] pairs instead of raw arrays? Do the lookup using the URL and off you go.

Comments

0

Code should be

 for (int i = 0; i <temp.length; i++)       
 {          
    urls[i] = temp[i];      
 }

Array index start from 0 to length-1

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.