在过去的几个小时里,我一直在试图找出这个股票查看器应用程序的问题。我昨天让它工作得很好,今天做了一些编辑,然后尝试将其恢复到原始形式。现在,它不是打印StockClient
中列出的日期(2016 年 1 月 7 日至 2017 年 1 月 7 日)的所有股票价格,而是从 2 月开始。请帮忙!
股票查看器类:
import java.util.*;
import java.net.URL;
import java.net.URLConnection;
public class StockViewer
{
private ArrayList<String> dates;
private ArrayList<String> opens;
private ArrayList<String> highs;
private ArrayList<String> lows;
private ArrayList<String> closes;
private ArrayList<String> volumes;
private ArrayList<String> adjCloses;
private String sym;
private boolean success = false;
public StockViewer(String symbol)
{
dates = new ArrayList<String>();
opens = new ArrayList<String>();
highs = new ArrayList<String>();
lows = new ArrayList<String>();
closes = new ArrayList<String>();
volumes = new ArrayList<String>();
adjCloses = new ArrayList<String>();
sym = symbol;
}
public void viewStock(GregorianCalendar begin, GregorianCalendar fin)
{
while(!success)
{
String url = "http://chart.finance.yahoo.com/table.csv?s=" + sym +
"&a=" + begin.get(Calendar.MONTH) +
"&b=" + begin.get(Calendar.DAY_OF_MONTH) +
"&c=" + begin.get(Calendar.YEAR) +
"&d=" + fin.get(Calendar.MONTH) +
"&e=" + fin.get(Calendar.DAY_OF_MONTH) +
"&f=" + fin.get(Calendar.YEAR) +
"&g=d&ignore=.csv";
try
{
URL yhooFin = new URL(url);
URLConnection data = yhooFin.openConnection();
Scanner input = new Scanner(data.getInputStream());
if(input.hasNext()) // skip a line (that's the header)
input.nextLine();
// start reading data
while(input.hasNextLine())
{
String line = input.nextLine();
String[] elements = line.split(",");
success = true;
dates.add(elements[0]);
opens.add(elements[1]);
highs.add(elements[2]);
lows.add(elements[3]);
closes.add(elements[4]);
volumes.add(elements[5]);
adjCloses.add(elements[6]);
for(int count = 0; count < elements.length; count++)
{
if(count < 6)
System.out.print(elements[count] + " - ");
else
System.out.print(elements[count] + "n");
}
}
}
catch(Exception e)
{}
}
}
public void viewStockCurrent(GregorianCalendar c1, GregorianCalendar c2)
{
viewStock(c1, c2);
}
}
股票客户类别:
import java.util.*;
public class StockClient
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a stock symbol: ");
String symbol = scan.next();
StockViewer stock = new StockViewer(symbol);
GregorianCalendar start = new GregorianCalendar(2016, 1, 7);
GregorianCalendar end = new GregorianCalendar(2017, 1, 7);
stock.viewStock(start, end);
}
}
Month 属性从零开始,您在
GregorianCalendar start = new GregorianCalendar(2016, 1, 7);
GregorianCalendar end = new GregorianCalendar(2017, 1, 7);
有关详细信息,请参阅日历.MONTH。
用于获取和设置指示月份的字段编号。这是特定于日历的值。公历和儒略历中一年的第一个月是
JANUARY
0
;最后一个取决于一年中的月数。