0

I have a method that takes in an ArrayList of urls. The url in each element is checked to see if it connects. If the status code is not 200, the url is added to a count as a broken link. As you can see I also print the url and the stauts code with message too.

What I would like to do is makes counts for each different status code that is not 200.

So I would have my total number of broken links and also a count for each instance of a status code that is not 200. e.g. if there are 5 http statuses of 404 they will be counted separately, as will any other status code that is not 200.

I do not want to have to hard code in every status code into my code as a count variable, I was wondering is there a way the code could create a new count variable for each different status?

Here is my current method:

public static void LinkSorter(List x){
        System.out.println("processing...");
        int brokenLinkCount = 0;

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

            try{

                URL url = new URL((String) x.get(i));
                URLConnection conn = url.openConnection();
                conn.connect();
                HttpURLConnection httpCon = (HttpURLConnection)conn;
                int statusCode = httpCon.getResponseCode();
                String statusMessage = httpCon.getResponseMessage();

                if(statusCode != 200){
                    System.out.println((String) x.get(i) + statusCode + statusMessage);
                    brokenLinkCount++;
                }

              catch(MalformedURLException e) {
                System.out.println("MalformedURL Exception");
                System.out.println(e.getMessage());
                brokenLinkCount++;

            } catch(IOException e){
                System.out.println("IOException");
                System.out.println(e.getMessage() + "IOException");
                brokenLinkCount++;                  
            }   

        }

        if(brokenLinkCount == 0){
            System.out.println("There are no broken links.");
        }else if(brokenLinkCount == 1){
            System.out.println("There is 1 broken link.");
        }else{
        System.out.println("There are " + brokenLinkCount + " broken links.");
        } 

    }

}

2 Answers 2

1

You could use a MultiMap, with the key being the status code and the values being the broken links. That pretty much automatically gets you both:

  • Each individual non-200 status code (as keys)
  • The count of the 'broken' links associated with each code (map.get(code).size())

Pseudo-ish code:

// declare a multimap, somewhere before you need to first access it
Multimap<Integer, String> brokenLinkMap = ArrayListMultiMap.create();

// use the map, in your current if block
if(statusCode != 200){
    System.out.println((String) x.get(i) + statusCode + statusMessage);
    brokenLinkMap.put(statusCode, url.toExternalForm()); // <-- adding to the map!
}

// after all links have been processed, you can dump the map contents
for(final Integer key : brokenLinkMap.keys()) {
    System.out.printf("Broken link count for status %s: %s\n", key, brokenLinkMap.get(key).size());
}

Alternatively, you could use all standard JDK classes:

// declare a map of lists, somewhere before you first need to access it
Map<Integer, List<String>> brokenLinkMap = new HashMap<Integer, List<String>>(4);

// use the map, in your current if block
if(statusCode != 200){
    System.out.println((String) x.get(i) + statusCode + statusMessage);
    if(! brokenLinkMap.containsKey(statusCode)) {
        List<String> brokenLinkList = new ArrayList<String>(8);
        brokenLinkMap.put(statusCode, brokenLinkList);
    }

    brokenLinkMap.get(statusCode).add(url.toExternalForm()); // <-- adding to the map!
}

// after all links have been processed, you can dump the map contents
for(final Integer key : brokenLinkMap.keys()) {
    System.out.printf("Broken link count for status %s: %s\n", key, brokenLinkMap.get(key).size());
}
12
  • I edited my code above with what you suggest. Just leanring coding, not sure why it won't work. Eclipse telling me I need an initialiser after I added interface to the multimap. Apr 22, 2013 at 11:29
  • I'm aware that my brokenLinkCount isn't relevant anymore. Apr 22, 2013 at 11:30
  • You should revert your edit, because it makes it harder to understand your original question. You can't use the code I've shown in one continuous block, I will add comments to help you integrate. Please read the code and more importantly, understand it. Then if you have follow-up questions post a comment.
    – Perception
    Apr 22, 2013 at 11:32
  • When I declare the multimap, it's telling me it can't be resolved to a type. That I need to create a class or an interface. Apr 22, 2013 at 11:43
  • You need to download the Guava Collections JAR and include it in your project build path.
    – Perception
    Apr 22, 2013 at 11:55
1

I guess you could use java.util.Map, using the Status Code as key and the value as your counter.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.