java - Exception to Number Format Exception with "D" and "F"? -


i have run strange problem in code. have simple temperature converter user enters temperature in celsius and, after pressing "convert", temperature in fahrenheit shown. if user not enter valid (anything not number or decimal) error dialog box shown. code:

btnconvert.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             try {                 string tempfahr = (string) enterdegreesc.gettext();                 double tempf = double.valueof(tempfahr);                 double tempfconverted = tempf * 1.8 +32;                             displaydegreesf.settext(tempfconverted + " farenheit");             }             catch (numberformatexception nfe) {                 joptionpane.showmessagedialog(frmtemperatureconverter, "please enter number.", "conversion error", joptionpane.error_message);                                 }                        }     }); 

pretty straight forward , simple code , works except 1 thing. when enter combination of number followed letters "f" or "d", no error dialog shown , temperature in fahrenheit calculated using digit in front on letter. happens "d" , "f" (and "d" , "f") , not other letter. stumped on one. why these 2 letters when placed after digit cause exceptions not thrown , calculation proceed?

some languages allow put letters after number literals signify type is.

if wrote 12.3, might not know whether float or double(or it'd have infer or cast it).

your number parser must picking on these letters.

  • 12.3d 12.3 double
  • 12.3f 12.3 float

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -