繁体   English   中英

如何在Java中打印我的Hashmap

[英]How do I print out my Hashmap in Java

好的,我有一个股票类的哈希表,该哈希表基本上具有用户可以输入的来自Yahoo的不同股票的数据。 每次他们输入新股票时,我都会将该Stock类添加到哈希图中,但我不知道如何打印出整个哈希图并显示到目前为止输入的所有内容

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];

        }
    }
}

您需要遍历地图并打印每个条目所需的内容。 遍历值的最简单方法是:

for (Stock stock : mappin.values()) {
    System.out.println(stock.toString());
}

这当然假设您的Stock类具有toString()的有意义的输出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM