线程“AWT-EventQueue-1”中出现异常java.lang.NumberFormatException

发布于 2024-09-03 07:37:14 字数 286 浏览 3 评论 0原文

我的小程序有这个问题。它在产生错误之前只绘制一行。

这是我的代码: http://www.so.pastebin.com/RkG5YHVQ

这是错误: http://www.so.pastebin.com/z1qWpFS6

I have this problem with my applet. It only paints ONE row before it produces the error.

Here's my code: http://www.so.pastebin.com/RkG5YHVQ

Here's the error: http://www.so.pastebin.com/z1qWpFS6

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

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

发布评论

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

评论(4

柠栀 2024-09-10 07:37:14

只需将 38 行更改为:

int line = Integer.parseInt(src.next().trim());

这将从导致代码错误的数字字符串中删除任何空格。

Simply change line 38 to:

int line = Integer.parseInt(src.next().trim());

This will trim off any whitespace from the number string that is causing the error in the code.

孤独岁月 2024-09-10 07:37:14

看起来 dan.txt 在零之前包含一个额外的换行符。

Looks like dan.txt contains an extra newline before a zero.

猫九 2024-09-10 07:37:14

看起来您的扫描仪没有使用空格作为分隔符,并且 Integer.parseInt(src.next()); 在它找到的第一个换行符上被阻塞。

您可以尝试使用 src.useDelimiter("[,\\s]+") 之类的方法来使用一个或多个空格和逗号字符的任意分组作为分隔符。

Looks like your scanner is not using whitespace as a delimiter, and Integer.parseInt(src.next()); is choking on the first newline it finds.

You could try something like src.useDelimiter("[,\\s]+") to use any grouping of one or more whitespace and comma characters as delimiters.

伴梦长久 2024-09-10 07:37:14

以下是开始调试此问题的方法:

自下而上地通读堆栈跟踪并注意 Java 源文件开始出现的位置。对于您的情况:

Inventory.paint(Inventory.java:51)

现在您可以开始使用几种不同的方法来调试该行。首选方法是将调试器附加到正在运行的程序实例,然后查看该行发生了什么。如果您不能这样做,只需添加一个 try catch 块来捕获该代码行周围的异常并打印出实际值与预期值。

Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: For inpu
t string: "
0"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
        at java.lang.Integer.parseInt(Integer.java:449)
Tile0   at java.lang.Integer.parseInt(Integer.java:499)

        at Inventory.paint(Inventory.java:51)  <===== Your code shows up!
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
        at sun.awt.RepaintArea.paint(RepaintArea.java:224)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:306)
        at java.awt.Component.dispatchEventImpl(Component.java:4706)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
Row: 1 successfully painted.

Here's how you start to debug this problem :

Read through the stack trace bottom up and notice where your Java source files start to show up. In your case:

Inventory.paint(Inventory.java:51)

Now you can start to debug this line using several different methods. The preferred approach is to attach a debugger to a running instance of your program and see what happens at this line. If you can't do that, simply add a try catch block to capture the exception around that line of code and print out the actual value vs the expected value.

Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: For inpu
t string: "
0"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
        at java.lang.Integer.parseInt(Integer.java:449)
Tile0   at java.lang.Integer.parseInt(Integer.java:499)

        at Inventory.paint(Inventory.java:51)  <===== Your code shows up!
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
        at sun.awt.RepaintArea.paint(RepaintArea.java:224)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:306)
        at java.awt.Component.dispatchEventImpl(Component.java:4706)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
Row: 1 successfully painted.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文