Guest User

Untitled

a guest
Mar 4th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. package com.dailyprogrammer;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7. import java.util.ArrayList;
  8. import static java.lang.System.out;
  9.  
  10. public class Intermediate17 {
  11.  
  12.     /**
  13.      * @param args
  14.      * @throws IOException
  15.      */
  16.     public static void main(String[] args) throws IOException {
  17.  
  18.         out.println("Connecting...");
  19.         URL url = new URL("http://www.reddit.com/r/dailyprogrammer/comments/qhe4u/342012_challenge_17_intermediate/");
  20.         BufferedReader in = new BufferedReader(new InputStreamReader(
  21.                 url.openStream()));
  22.  
  23.         String inputLine;
  24.         ArrayList<String> quotes = new ArrayList<String>();
  25.        
  26.        
  27.         while ((inputLine = in.readLine()) != null) {          
  28.             if (inputLine.length() > 2) {
  29.                 int startQuote = inputLine.indexOf("&quot;");
  30.                 int endQuote = inputLine.indexOf("&quot;", startQuote+1);
  31.                 if (startQuote != -1) {
  32.                     quotes.add(inputLine.substring(startQuote+6, endQuote));
  33.                 }
  34.             }
  35.         }
  36.        
  37.         out.println("Quotes on webpage enclosed in \"&quot;\" tag as per example:");
  38.         out.println("Webpage: "+url);
  39.         out.println("Number of quotes = " + quotes.size());
  40.  
  41.         for (int i = 0; i < quotes.size(); i++) {
  42.             out.printf("%d: ", i+1);
  43.             out.println(quotes.get(i));
  44.         }
  45.  
  46.     }
  47.  
  48. }
Add Comment
Please, Sign In to add comment