aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I/O code exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I/O code exception" Watch "I/O code exception" New topic
Author

I/O code exception

gurneeraj singh
Ranch Hand

Joined: Mar 19, 2008
Posts: 80


If we run the above code by typing a integer value say 3 it produces NumberFormatException in Line 1. Why?
When we know that string "3" can be changed into integer value 3 by parseInt() method without any exception.

[ March 26, 2008: Message edited by: gurneeraj singh ]

SCJP 5.0 93%<br /> <br />SCWCD 5.0 97 %
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42923
    
  68
What is the value of "str" when parseInt is called?
gurneeraj singh
Ranch Hand

Joined: Mar 19, 2008
Posts: 80
value of str was 3 when parseInt was called
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42923
    
  68
Just a single character? No white space around it? That would sound like a bug.
gurneeraj singh
Ranch Hand

Joined: Mar 19, 2008
Posts: 80
yes, just a single chracter "3" no white space. Line just above Line 1 prints 3 but when we use parseInt it throws exception.
gurneeraj singh
Ranch Hand

Joined: Mar 19, 2008
Posts: 80
Thanks Ulf,
Acctually I got the answer from your reply, there is whitespace with string str because of the empty char array elements when converted to string str. There are two ways to get rid of this
1) Use str=str.trim(); just above the Line 1
2) Or set the char array size to 1.
[ March 26, 2008: Message edited by: gurneeraj singh ]
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi. Tell you the way to test String value in a program..!!
always try to print String value like this
System.out.println("["+ str + "]");
so Atmost you know whether string was blank,containing space etc..!!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by gurneeraj singh:
... 2) Or set the char array size to 1.


What if the user types '33'?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
gurneeraj singh
Ranch Hand

Joined: Mar 19, 2008
Posts: 80
Dear Ben,
You are right so we have to opt first option i.e. to use trim method.
gurneeraj singh
Ranch Hand

Joined: Mar 19, 2008
Posts: 80
I think sunny's method is also good to check whether string has whitespace or not
gurneeraj singh
Ranch Hand

Joined: Mar 19, 2008
Posts: 80


Dear Ben,
I think if we make following changes to code at line a1 & line a2. We are somewhat able to solve the problem of exception at line 1. Please suggest!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I/O code exception