0

さて、私は Stock クラスのハッシュマップを持っています。これは基本的に、ユーザーが入力できる 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];

        }
    }
}
4

1 に答える 1

0

マップを繰り返し処理し、エントリごとに必要なものを出力する必要があります。値を反復処理する最も簡単な方法は次のとおりです。

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

もちろん、これは Stock クラスが toString() に対して意味のある出力を持っていることを前提としています

于 2013-03-03T18:10:02.610 に答える