1

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 main() part of the following every 5 seconds (or whatever time):

This code works:

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]+"&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();
}
}

Now, I've tried incorporating the above into the time program below. Have tried throwing exceptions and playing with try-catch but I'm out of my element at this point. Let me know.

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 <= 5) {

 String[] stocks={"GOOG","MSFT"}; //
    URL yahoofinance = new URL("http://finance.yahoo.com/d/quotes.csv?        

s="+stocks[0]+"+"+stocks[1]+"&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

    }
}
}

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:

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]+"&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
4

2 に答える 2

4

これは私のために働く:

        try
        {
            String[] stocks =
            {
                "GOOG", "MSFT"
            }; //
            URL yahoofinance = new URL("http://finance.yahoo.com/d/quotes.csv?s=" + stocks[0] + "+" + stocks[1] + "&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();
        } catch (IOException ex)
        {
            System.out.println("Oops bad things happens");
        }
于 2012-11-15T19:06:57.543 に答える
2

「MyTask」で定義した run メソッドは例外をスローしません。ただし、実行するコードは「new URL()」などの例外をスローします。

メインで機能する理由は、例外を再スローできるためです。

これらの例外は処理されないため、コンパイラは (当然のことながら) それについて不平を言っています。

問題のある呼び出しを try/catch ブロックで囲む必要があります。例えば:

URL yahoofinance = null;
try{
  yahoofinance = new URL( "http://finance.yahoo.com/d/quotes.csv?" );
} catch( MalformedURLException ex ) {
  // print error or throw a new error or whatever
  throw new Error( "I cannot deal with this problem" );
}

投稿のコンパイル例外ごとに同様のことを行います。

于 2012-11-15T18:15:29.877 に答える