views:

192

answers:

4

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

+1  A: 

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

Matt McHenry
Here is dan.txt0,1,2,3,4,2,3,3,0,1,2,3,4,2,3,3,0,1,2,3,4,2,3,3,0,1,2,3,4,2,3,3,0,1,2,3,0,2,3,3,0,1,2,3,4,2,3,3,0,1,2,3,4,2,3,3,0,1,2,3,4,2,3,3,I don't think so. :\
Dan
Yeah, you were right.
Dan
+1  A: 

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.

jjnguy
This works! Thanks!! :)
Dan
@Dan No problem, glad to help.
jjnguy
+1  A: 

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.

spong
This works as well! Gotta love regex.
Dan
+1  A: 

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.
Amir Afghani
Got it, thanks.
Dan