• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger


Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, could you help me understand this code, please?

The output is "trouble 12.3". So, f becomes 12.3, then it is changed into a String s "12.3" and then "12.3" is passed into parseInt method. Why does it throw NumberFormatException if when I write Integer x = Integer.parseInt("12.3"); System.out.println(x); the output will be 12 (no exception)?

Thank you,

Posts: 70651
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you looked up the documentation for Integer#parseInt()? That tells you the format of String permissible as an argument. I don't know how you got the following code to run normally:-

jshell> Integer x = Integer.parseInt("12.3"); System.out.println(x);
|  Exception java.lang.NumberFormatException: For input string: "12.3"
|        at NumberFormatException.forInputString (NumberFormatException.java:68)
|        at Integer.parseInt (Integer.java:652)
|        at Integer.parseInt (Integer.java:770)
|        at (#1:1)

Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic