在过去的几个小时里,我一直试图找出这个股票查看器应用程序的问题。我昨天工作得非常好,今天进行了一些编辑,然后尝试将其恢复原状。现在,不是从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);
}
}
答案 0 :(得分:0)
月份属性为零,
中的值错误GregorianCalendar start = new GregorianCalendar(2016, 1, 7);
GregorianCalendar end = new GregorianCalendar(2017, 1, 7);
有关详细信息,请参阅Calendar.MONTH。
get和set的字段编号,表示月份。这是特定于日历的值。 格里高利和朱利安日历中一年中的第一个月是
JANUARY
,0
;最后一个取决于一年中的月数。