• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NumberFOrmat.format question.

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all , this question is derived from k&b book practice:



why line 1 will cause the exception???
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

It's because s = "123.456xyz" is not a number.
If you pass a number, this will work.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method format() throws exception because it expects an argument which can be cast to one of the numerical wrapper classes (or one of BigInteger, BigDecimal, AtomicInteger and AtomicLong). A string argument, even if containing only digits is bound to generate an IllegalArgumentException.

The following should work:

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

Aditya Jha wrote:Method format() throws exception because it expects an argument which can be cast to one of the numerical wrapper classes (or one of BigInteger, BigDecimal, AtomicInteger and AtomicLong). A string argument, even if containing only digits is bound to generate an IllegalArgumentException.

The following should work:



hi, i have checked the API:

String format(double number)
Specialization of format.
abstract StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos)
Specialization of format.
String format(long number)
Specialization of format.
abstract StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos)
Specialization of format.
StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos)
Formats an object to produce a string.


i cannot understand there is no one of the function above is accepting string as argument, why the compiler allow the code above to run.

Thanks.
 
Viktor Pergjoka
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.text.NumberFormat extends java.text.Format and it inherits the format(Object obj) method. You pass a String in and since String is an Object, like all others, the method accept it and cast the String.
The method than returns a String and this String is used in the format method.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic