简体   繁体   中英

Retrieving Stock Quotes using Eclipse - Error: The value of the local variable is not used

I'm trying out a java application to retrieve stock quotes using Yahoo API. http://greatwebguy.com/programming/java/stock-quote-and-chart-from-yahoo-in-java/

No amendments to the sample code above. I've just added a main class.

public class Main {

    public static void main (String[] args) {

        StockBean stock = StockTickerDAO.getInstance().getStockPrice("GOOG");     
    }
}

I'm unable to execute the main class due to: The value of the local variable stock is not used.

Can anyone spot what I'm missing? Thank you in advance!

You're not doing anything wrong . You're just not using the retrieved stock figures.

Take a peak inside the StockBean class to see what methods it exposes. I am assuming something like StockBean.getPrice() will be publicly exposed. Just use that as so:

StockBean stock = StockTickerDAO.getInstance().getStockPrice("GOOG");
System.out.println("Stock Price: " + stock.getPrice());

For debugging / logging purposes, you might want a convenient approach that spits out the contents of the entire bean. That can be done if your StockBean had the toString method overridden.

If that was the case, you could've just done the below and it would've neatly enlisted all the properties.

System.out.println(stock);

If you can edit the StockBean class, I suggest you implement toString by using Eclipse or by hand.

Yahoo discontinued it's stock quote service after it was acquired by Verizon. Intrinio is the alternative right now. There is a sample java program in GitHub at https://github.com/pmkent/intrinio-java-sample

It's just what the message said: you don't do anything with the variable stock . Normally this is a warning but it might have beed changed to be an error. To fix it, use the variable or just don't introduce it.

Alternatively, adapt the Eclipse settings to make this a warning or even ignore it, or add the @SuppressWarnings("unused") annotation to the main method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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