Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

I/O code exception

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the value of "str" when parseInt is called?
 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
value of str was 3 when parseInt was called
 
Ulf Dittmer
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a single character? No white space around it? That would sound like a bug.
 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..!!
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



What if the user types '33'?
 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ben,
You are right so we have to opt first option i.e. to use trim method.
 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think sunny's method is also good to check whether string has whitespace or not
 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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!

 
reply
    Bookmark Topic Watch Topic
  • New Topic