• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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: 43081
77
  • 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: 43081
77
  • 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!

 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic