简体   繁体   English

使用Eclipse检索股票报价 - 错误:未使用局部变量的值

[英]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. 我正在尝试使用Yahoo API检索股票报价的java应用程序。 http://greatwebguy.com/programming/java/stock-quote-and-chart-from-yahoo-in-java/ 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. 由于以下原因,我无法执行主类: 不使用局部变量stock的值。

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. StockBean类中StockBean一个高峰,看看它暴露了哪些方法。 I am assuming something like StockBean.getPrice() will be publicly exposed. 我假设像StockBean.getPrice()这样的东西会被公开曝光。 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. 出于调试/日志记录的目的,您可能需要一种方便的方法来吐出整个bean的内容。 That can be done if your StockBean had the toString method overridden. 如果您的StockBean重写了toString方法,则可以执行此操作。

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. 如果您可以编辑StockBean类,我建议您使用Eclipse或手工实现toString

Yahoo discontinued it's stock quote service after it was acquired by Verizon. 在被Verizon收购后,雅虎停止了它的股票报价服务。 Intrinio is the alternative right now. Intrinio现在是替代品。 There is a sample java program in GitHub at https://github.com/pmkent/intrinio-java-sample GitHub中有一个示例java程序, 网址https://github.com/pmkent/intrinio-java-sample

It's just what the message said: you don't do anything with the variable stock . 这就是消息所说的内容:你对变量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. 或者,调整Eclipse设置以使其成为警告或甚至忽略它,或者将@SuppressWarnings("unused")注释添加到main方法。

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

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