好的,所以我有一个类库存的哈希映射,它基本上有关于用户可以输入的来自雅虎的不同股票的数据。每次他们输入新股票时,我都会将Stock类添加到hashmap中,我不知道如何打印整个hashmap并显示到目前为止输入的所有内容
public class AS4stocks {
static Map<String, Stock> mappin = new HashMap<String, Stock>();
public static void main(String[] args) throws IOException {
int menuchoice;
do {
Scanner in1 = new Scanner(System.in);
System.out
.println("What would you like to do \n1) Print table\n2) Add a stock\n3) Do something else");
menuchoice = in1.nextInt();
switch (menuchoice) {
case 1:
System.out.println(mappin);
break;
case 2:
System.out.print("Enter the stock's ticker symbol\n");
String ticker = in1.next();
addstock(ticker);
break;
case 3:
break;
}
} while (menuchoice != 0);
}
private static void Printtable(Map<String, Stock> mappin) {
fo
}
private static void addstock(String ticker) throws IOException {
URL url = new URL("http://download.finance.yahoo.com/d/quotes.csv?s="
+ ticker + "&f=snd1ohgpvwm3m4&e=.csv");
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
Object[] otable = new String[11];
int counter = 0;
while ((line = br.readLine()) != null) {
String str = line;
StringTokenizer st = new StringTokenizer(str, ",\"");
while (st.hasMoreTokens()) {
String adder = st.nextToken();
otable[counter] = adder;
if (counter == 0) {
System.out.println("-------------------" + adder
+ "-------------------");
System.out.print("Ticker: ");
}
if (counter == 2) {
System.out.print("Company: ");
}
if (counter == 3) {
System.out.print("Open: ");
}
if (counter == 4) {
System.out.print("High: ");
}
if (counter == 5) {
System.out.print("Low: ");
}
if (counter == 6) {
System.out.print("Close: ");
}
if (counter == 7) {
System.out.print("Volume: ");
}
if (counter == 8) {
System.out.print("52 Week Range: ");
}
if (counter == 9) {
System.out.print("50 Day Average: ");
}
if (counter == 10) {
System.out.print("200 Day Average: ");
}
System.out.println(adder);
counter++;
}
Stock snew = new Stock(otable);
mappin.put(ticker, snew);
}
System.out.println();
}
static class Stock {
String compname;
String ticker;
String date;
String open;
String high;
String low;
String close;
String volume;
String range;
String average50;
String average200;
public Stock(Object otable[]) {
compname = (String) otable[0];
ticker = (String) otable[1];
date = (String) otable[2];
open = (String) otable[0];
high = (String) otable[1];
low = (String) otable[2];
close = (String) otable[3];
volume = (String) otable[4];
range = (String) otable[5];
average50 = (String) otable[6];
average200 = (String) otable[7];
}
}
}
答案 0 :(得分:0)
您需要遍历地图并打印每个条目所需的内容。迭代值的最简单方法是:
for (Stock stock : mappin.values()) {
System.out.println(stock.toString());
}
当然,假设您的Stock类对toString()
有有意义的输出