title: Java URL grab stock quote on timer
body: <p>Someone suggested I post this in stackoverflow instead of stackexchange, so here I am. I'm trying to make a simple stock-ticker of sorts. Just experimenting with free time. Anyways, I'm trying to simply run the <code>main()</code> part of the following every 5 seconds (or whatever time):</p>
  
  <p>This code works: </p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  
  public class URLConnectionReader {
  public static void main(String[] args) throws Exception {
  String[] stocks={"GOOG","MSFT"}; //
      URL yahoofinance = new URL("http://finance.yahoo.com/d/quotes.csv? 
  s="+stocks[0]+"+"+stocks[1]+"&amp;f=hg");
      URLConnection yc = yahoofinance.openConnection();
      BufferedReader in = new BufferedReader(new InputStreamReader(
                                  yc.getInputStream()));
      String inputLine;
      while ((inputLine = in.readLine()) != null) 
          System.out.println(inputLine);
            in.close();
  }
  }
  </code></pre>
  
  <p>Now, I've tried incorporating the above into the time program below. Have tried throwing exceptions and playing with <code>try-catch</code> but I'm out of my element at this point. Let me know. </p>
  
  <pre><code>import java.util.Timer;
  import java.util.TimerTask;
  import java.net.*;
  import java.io.*;
  
  public class StockPrinter {
  
  public static void main(String[] args) throws Exception {
      //1- Taking an instance of Timer class.
      Timer timer = new Timer("Printer");
  
      //2- Taking an instance of class contains your repeated method.
      MyTask t = new MyTask();
  
  
      //TimerTask is a class implements Runnable interface so
      //You have to override run method with your certain code black
  
      //Second Parameter is the specified the Starting Time for your timer in
      //MilliSeconds or Date
  
      //Third Parameter is the specified the Period between consecutive
      //calling for the method.
  
      timer.schedule(t, 0, 2000);
  
  }
  }
  
  class MyTask extends TimerTask {  /*extends implies that MyTask has all    
  variables/properties of TimerTask */
  //times member represent calling times.
  private int times = 0;
  
  
  public void run() {
     // String[] stocks={"GOOG","MSFT"};
      times++;
  
  
      if (times &lt;= 5) {
  
   String[] stocks={"GOOG","MSFT"}; //
      URL yahoofinance = new URL("http://finance.yahoo.com/d/quotes.csv?        
  
  s="+stocks[0]+"+"+stocks[1]+"&amp;f=hg");
       URLConnection yc = yahoofinance.openConnection();
      BufferedReader in = new BufferedReader(new InputStreamReader(
                                  yc.getInputStream()));
      String inputLine;
      while ((inputLine = in.readLine()) != null) 
          System.out.println(inputLine);
            in.close();        
  
      } else {
          System.out.println("Timer stops now...");
  
          //Stop Timer.
          this.cancel();
          System.exit(0); //added:should quit program
  
      }
  }
  }
  </code></pre>
  
  <p>Note: I've grabbed the main chunks of these codes from either Java's website or perhaps in some forum, so I apologize if any of it is recognised. Not passing the heavy lifting off as my own by any means. Just want to get this to work. Here are the compile errors: </p>
  
  <pre><code>StockPrinter.java:43: unreported exception java.net.MalformedURLException; must
  be caught or declared to be thrown
      URL yahoofinance = new URL("http://finance.yahoo.com/d/quotes.csv?s="+st
  ocks[0]+"+"+stocks[1]+"&amp;f=hg");
                         ^
  StockPrinter.java:44: unreported exception java.io.IOException; must be caught o
  r declared to be thrown
      URLConnection yc = yahoofinance.openConnection();
                                                    ^
  StockPrinter.java:46: unreported exception java.io.IOException; must be caught o
  r declared to be thrown
                                  yc.getInputStream()));
                                                   ^
  StockPrinter.java:48: unreported exception java.io.IOException; must be caught o
  r declared to be thrown
      while ((inputLine = in.readLine()) != null)
                                     ^
  StockPrinter.java:50: unreported exception java.io.IOException; must be caught o
  r declared to be thrown
            in.close();
                    ^
  5 errors
  </code></pre>