How can I make a float throw an exception, when entered?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jerick
    New Member
    • Feb 2012
    • 9

    How can I make a float throw an exception, when entered?

    I have a GUI that I use as a calculator. When I enter in anything other than numbers, it throws a NumberFormatExc eption, as intended. But when I enter in a number such as "23423f" the program seems to think I'm entering a float and doesn't throw an exception, because of the f at the end, even though all other letters throw an exception.

    Currently, all I'm doing is putting the input section into a try catch block, where I parse it into a double, and having it catch a NumberFormatExc eption if there is one (because only numbers can be parsed into doubles), and another catch block to throw an IllegalArgument Exception from an if statement for any input less than 0.

    That's just a guess, but I don't know why else only f at the end wouldn't throw an exception.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    It's not just f.
    l at the end makes it a long and d makes it a double.
    See http://docs.oracle.com/javase/tutori...datatypes.html

    Comment

    Working...