有人建议我将其发布在 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