0

Okay so I have a hashmap of the class Stock, which basically has data about different stocks from yahoo that a user can enter. Each time they enter a new stock, I add that Stock class to the hashmap, and I don't know how to print out the whole hashmap and display everything entered so far

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

        }
    }
}
kapex
  • 28,903
  • 6
  • 107
  • 121
Logan
  • 31
  • 1
  • 2
  • 1
    Take a look at this: http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html#entrySet%28%29 should get you started – Jason Sperske Mar 03 '13 at 18:08
  • Take a look here: [how-do-i-iterate-over-each-entry-in-a-map](http://stackoverflow.com/questions/46898/how-do-i-iterate-over-each-entry-in-a-map) – Pshemo Mar 03 '13 at 18:09
  • 1
    Very clumsy code. I think you want to go with basic Java tutorials. – Lion Mar 03 '13 at 18:09

1 Answers1

0

You'll need to iterate through the map and print what you want for each entry. Easiest way to iterate through the values would be:

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

This assumes of course your Stock class has meaningful output for toString()

frostmatthew
  • 3,260
  • 4
  • 40
  • 50
  • I tried this and all it prints out is - AS4stocks$Stock@78b5f53a AS4stocks$Stock@71f6f0bf – Logan Mar 03 '13 at 20:18
  • 1
    Your Stock class has several attributes, if you add a `toString()` method that returns the output you want than this answer will work for you. Perhaps something like `public String toString() {return "("+ticker+") "+open;}` – Jason Sperske Mar 03 '13 at 22:06
  • When you iterate through the values in the map you can output whatever you want from the Stock class. My use of toString was just an example under the assumption you would override toString to output what was desired. Without you providing any further info on what output you want from the Stock class the only way to answer your posted question is that you can iterate through the map and access whatever public methods (or members) you desire. – frostmatthew Mar 04 '13 at 02:04