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

发布于 2025-01-03 01:53:56 字数 535 浏览 0 评论 0原文

我正在尝试使用 Java 应用程序使用 Yahoo API 检索股票报价。 http://greatwebguy.com/programming/ java/stock-quote-and-chart-from-yahoo-in-java/

对上面的示例代码没有修改。我刚刚添加了一个主类。

public class Main {

    public static void main (String[] args) {

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

由于以下原因,我无法执行主类: 未使用局部变量 stock 的值。

有人能发现我缺少什么吗? 先感谢您!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自演自醉 2025-01-10 01:53:56

你没有做任何错误的事情。您只是没有使用检索到的库存数据。

StockBean 类内部进行观察,看看它公开了哪些方法。我假设像 StockBean.getPrice() 这样的东西将会被公开。只需这样使用:

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

出于调试/日志记录的目的,您可能需要一种方便的方法来吐出整个 bean 的内容。如果您的 StockBean 覆盖了 toString 方法,则可以完成此操作。

如果是这种情况,您只需执行以下操作,它就会整齐地列出所有属性。

System.out.println(stock);

如果您可以编辑 StockBean 类,我建议您使用 Eclipse 或手动实现 toString

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.

如梦亦如幻 2025-01-10 01:53:56

这正是消息所说的:您不对变量 stock 执行任何操作。通常这是一个警告,但它可能已更改为错误。要修复它,请使用变量或不引入它。

或者,调整 Eclipse 设置以使其成为警告,甚至忽略它,或者将 @SuppressWarnings("unused") 注释添加到 main 方法。

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.

掩耳倾听 2025-01-10 01:53:56

雅虎被 Verizon 收购后停止了股票报价服务。 Intrinio 是目前的替代方案。 GitHub 中有一个示例 Java 程序 https://github.com/pmkent/intrinio-java -样本

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文