• 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

problematic run-time errors

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am as green as can be w/code, and have been kind of thrown in the fire with an assignment from school. It has been turned in, but I would like to better understand what issues I am running into. When I enter an integer for tax rate it loops output. When a decimal is entered this:

java.lang.NumberFormatException: For input string: "0.5"
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:477)
at java.lang.Integer.parseInt(Integer.java:518)
at Project.AccountIn(Project3.java:63)
at Project.appMain(Project3.java:40)
at Project3.main(Project3.java:17)

Sorry for the length but here is the problematic code: (A BIG THANKS!!)

[edit]Add code tags. CR[/edit]
[ November 09, 2008: Message edited by: Campbell Ritchie ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The Integer class is for integers -- which is for whole numbers. "1.5" is not a whole number, hence, not an integer.

Henry
[ November 08, 2008: Message edited by: Henry Wong ]
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please Use Code Tags.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
many languages distinguish between integers (numbers with no fractional part) and floating point decimal numbers (numbers WITH a fractional part, although the fractional part may be zero - 5.0, for example). In fact, there are even different types for each of those, depending on how big a number you need to store vs. how much memory they use

I don't know the reason why they do this, but I assume it has to do with memory from back when memory was EXPENSIVE (my father talked about a computer Bell Labs bought where memory was $1 million for 1 megabyte).

In any case, as Henry has pointed out, you are using the "Integer" method to parse a string. Since it is expecting an integer to be represented in the string, it doesn't know how to handle a decimal point. Just like it wouldn't know how to handle "fred". If you want to parse a floating point, you need to use a different class than the Integer and it's parstInt() method.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always thought it is because it means you can store different numbers in different formats, eg
  • Two's complement-Java int, long, short, byte.
  • Unsigned binary-Java char, C++ unsigned int, C# uint
  • IEEE754-Java float and double
  • Something else, eg C# decimal.
  • And that means that arithmetic on different kinds of numbers can be done differently with different precision, etc.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Rob Prime:
    And please Use Code Tags.

    I have added the tags, so you can see how much better the code looks. By the way: you are using an unusual indentation convention: your code should start farther to the right than the {}.

    And (sorry I hadn't noticed it's your first post) welcome to JavaRanch
     
    Matt Riddoch
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Wow, thanks for the outpouring of help!! I am educating myself on the code tag issue, as I hope to become a regular contributor. I will crunch some of this info and hope for some better results!
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are welcome
    reply
      Bookmark Topic Watch Topic
    • New Topic