Results 1 to 8 of 8
  1. #1
    shy_ted is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default problem filling list with values from CSV feed

    afternoon,

    i have this feed...

    Java Code:
    package com.citi.blott;
    
    public class StockBean
    {   
        String ticker;   
        float price;   
        float change;   
        String chartUrlSmall;   
        String chartUrlLarge;   
        long lastUpdated;
        
    
        public void setTicker(String ticker) {
            this.ticker = ticker;
        }
        public String getTicker() {   
            return ticker;   
        }         
        public float getPrice() {   
            return price;   
        }   
        public void setPrice(float price) {   
            this.price = price;   
        }   
        public float getChange() {   
            return change;   
        }   
        public void setChange(float change) {   
            this.change = change;   
        }   
        public String getChartUrlSmall() {   
            return chartUrlSmall;   
        }   
        public void setChartUrlSmall(String chartUrlSmall) {   
            this.chartUrlSmall = chartUrlSmall;   
        }   
        public String getChartUrlLarge() {   
            return chartUrlLarge;   
        }   
        public void setChartUrlLarge(String chartUrlLarge) {   
            this.chartUrlLarge = chartUrlLarge;   
        }   
        public long getLastUpdated() {   
            return lastUpdated;   
        }   
        public void setLastUpdated(long lastUpdated) {   
            this.lastUpdated = lastUpdated;   
        }   
        
        public Object[] asObjects() {
    		return new Object[]{getTicker(), getPrice(), getChange()};
    	}
    }

    Next is the code that parses the CSV feed!

    Java Code:
    package com.citi.blott;
    
    import java.io.BufferedReader;   
    import java.io.InputStreamReader;   
    import java.net.URL;   
    import java.net.URLConnection;   
    import java.util.Date;   
    import java.util.HashMap;   
    
      
    public class StockTickerDAO {   
        //private static final Log log = LogFactory.getLog(StockTickerDAO.class);   
        private static final StockTickerDAO stockDAO = new StockTickerDAO();   
        private static HashMap<String, StockBean> stocks = new HashMap<String, StockBean>();   
        private static final long TWENTY_MIN = 1200000;   
      
        private StockTickerDAO() {}   
      
        public static StockTickerDAO getInstance() {   
            return stockDAO;   
        }   
      
        /**  
         *  
         * @param symbol  
         * @return StockBean  
         * will return null if unable to retrieve information  
         */  
        public StockBean getStockPrice(String symbol) {   
            StockBean stock;   
            long currentTime = (new Date()).getTime();   
            // Check last updated and only pull stock on average every 20 minutes   
            if (stocks.containsKey(symbol)) {   
                stock = stocks.get(symbol);   
                if(currentTime - stock.getLastUpdated() > TWENTY_MIN) {   
                    stock = refreshStockInfo(symbol, currentTime);   
                }   
            } else {   
                stock = refreshStockInfo(symbol, currentTime);   
            }   
            return stock;   
        }   
      
        //This is synched so we only do one request at a time   
        //If yahoo doesn't return stock info we will try to return it from the map in memory   
        private synchronized StockBean refreshStockInfo(String symbol, long time) {   
            try {   
                URL yahoofin = new URL("http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=sl1d1t1c1ohgv&e=.csv");   
                URLConnection yc = yahoofin.openConnection();   
                BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));   
                String inputLine;   
                while ((inputLine = in.readLine()) != null) {   
                    String[] yahooStockInfo = inputLine.split(",");   
                    StockBean stockInfo = new StockBean();   
                    stockInfo.setTicker(yahooStockInfo[0].replaceAll("\"", ""));   
                    stockInfo.setPrice(Float.valueOf(yahooStockInfo[1]));   
                    stockInfo.setChange(Float.valueOf(yahooStockInfo[4]));   
                    stockInfo.setChartUrlSmall("http://ichart.finance.yahoo.com/t?s=" + stockInfo.getTicker());   
                    stockInfo.setChartUrlLarge("http://chart.finance.yahoo.com/w?s=" + stockInfo.getTicker());   
                    stockInfo.setLastUpdated(time);   
                    stocks.put(symbol, stockInfo);   
                    break;   
                }   
                in.close();   
            } catch (Exception ex) {   
                System.out.println("Unable to get stockinfo for: " + symbol + ex);   
            }   
            return stocks.get(symbol);   
         }   
    }

    I need to pass a value from this other class

    Java Code:
    public class PriceListPopulator {
    
    	List<StockBean> getPricesToPopulate() {
    		List<StockBean> prices = new ArrayList<StockBean>();
    		[B][I][U]prices.add(new StockBean("msft"));[/U][/I][/B]		
    		return prices;
    	}

    When i change the constructor in the first example the like "stockInfo.setTicker(yahooStockInfo[0].replaceAll("\"", "")); " comes up with an error!!
    But i keep getting an error on the line thats highlighted, saying "cannot find symbol" Does anyone have any idea why?

    This method worked with an XML feed but isnt working for this CSV one.

    Basically the resulting list is added to a JTable elsewhere

    Thanks in advance
    Last edited by shy_ted; 10-11-2010 at 05:25 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default

    You need to show us the exact error.
    "Cannot find symbol" tells you what it can't find.

  3. #3
    shy_ted is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default

    cannot find symbol

    symbol constructor StockBean()
    location com.citi.blott.StockBean()

    it happens on this line

    Java Code:
    StockBean stockInfo = new StockBean();
    its something to do with these constructors...

    Java Code:
    public StockBean(String ticker) {
    		this.ticker = ticker;
    	}

    but without this there are more errors.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default

    You have a constructor declared that takes a String parameter.
    You are trying to call a constructor with no parameter at all....StockBean does not have such a constructor.

  5. #5
    shy_ted is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default

    yea, thats the thing, if i was to change it around, the errors would change too.

    unless i can have two constructors, but i dont think that would do what i want it to do, as it seems to be called from 2 places!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default

    Well, I can't help you there since I have no idea what your requirements are.
    You need to decide what it is StockBean requires to be created, and what can actually be supplied to StockBean at the point it's created.

  7. #7
    shy_ted is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default

    hmm, ok

    Basically, i have 3 class files working together and outputting the data to the output window as text, and i want them to be put into a JTable everytime the program is loaded.

    Previously i had it loading from XML and it worked fine, so i thought just changing the source that fills the arraylist would do it, but it wont. is there any other way that i can show you what i have?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default

    OK, using what you have already why don't you, instead of doing this:
    Java Code:
                    StockBean stockInfo = new StockBean();   
                    stockInfo.setTicker(yahooStockInfo[0].replaceAll("\"", ""));
    do this:
    Java Code:
    StockBean stockInfo = new StockBean(yahooStockInfo[0].replaceAll("\"", ""));
    ?

Similar Threads

  1. Replies: 2
    Last Post: 11-22-2009, 05:24 PM
  2. Replies: 3
    Last Post: 11-21-2009, 05:48 PM
  3. I have a problem filling out this method
    By kira137 in forum New To Java
    Replies: 6
    Last Post: 10-11-2009, 01:43 AM
  4. trying to set() values of in list of arraylist
    By alvations in forum New To Java
    Replies: 15
    Last Post: 10-13-2008, 09:35 PM
  5. Getting values of an Option List
    By mutuah in forum Advanced Java
    Replies: 0
    Last Post: 08-07-2007, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •