StockQuote.java
public class
StockQuote
{
public static String readHTML(String symbol)
{
In page = new
In("http://finance.yahoo.com/q?s=" + symbol);
String html = page.readAll();
return html;
}
public static double price(String html)
{
int p
= html.indexOf("yfs_l84", 0);
int from
= html.indexOf(">", p);
int to
= html.indexOf("", from);
String price = html.substring(from + 1,
to);
return
Double.parseDouble(price.replaceAll(",", ""));
}
public
static String name(String html)
{
int p
= html.indexOf("", 0);
int from = html.indexOf("Summary for ", p);
int to = html.indexOf("- Yahoo! Finance", from);
String name = html.substring(from + 12, to);
return name;
}
public static String date(String html)
{
int p = html.indexOf("", 0);
int from = html.indexOf(">", p);
int to = html.indexOf("-", from);
String date = html.substring(from + 1, to);
return date;
}
public static void main(String[] args)
{
String symbol = args[0];
String html = readHTML(symbol);
StdOut.printf("%.2f\n", price(html));
StdOut.println(name(html));
StdOut.println(date(html));
}
}
No comments:
Post a Comment