Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I sort these https status codes without hardcoding every single http status code into my java program?
    primarykey
    data
    text
    <p>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.</p> <p>What I would like to do is makes counts for each different status code that is not 200. </p> <p>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. </p> <p>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?</p> <p>Here is my current method:</p> <pre><code>public static void LinkSorter(List x){ System.out.println("processing..."); int brokenLinkCount = 0; for(int i=0; i&lt;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."); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload