1

有人建议我将其发布在 stackoverflow 而不是 stackexchange 中,所以我来了。我正在尝试制作一个简单的股票代码。只是尝试空闲时间。无论如何,我试图main()每 5 秒(或任何时间)简单地运行以下部分:

此代码有效:

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();
}
}

现在,我尝试将上述内容合并到下面的时间程序中。曾尝试抛出异常并玩弄,try-catch但此时我已经脱离了我的元素。让我知道。

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

    }
}
}

注意:我已经从 Java 的网站或某些论坛中获取了这些代码的主要部分,因此如果有任何被识别,我深表歉意。无论如何都不会把繁重的工作当作我自己的。只是想让这个工作。以下是编译错误:

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”中定义的运行方法不会抛出任何异常。但是,它执行的代码确实会引发异常,例如“new URL()”。

它在 Main 中工作的原因是因为它可以重新抛出任何异常。

由于这些异常没有得到处理,编译器(正确地)抱怨它。

您需要用 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 回答