Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with Float.parseFloat

 
roshni sivan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried the following code. Float.parseFloat("23d")does not throw an exception. The below code prints output as 23.0.
String s = new String ("23d");
try{
System.out.println(Float.parseFloat(s));

}
catch(Exception e){
System.out.println("not a number..");
}
How can I make Float.parseFloat("23d") throw an exception? Please help.


thanks.

roshni.
 
Krishnamurthy Sivakumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you want parseFloat method to throw an error, then you have to pass a string which cannot be parsed to a number like "abc". Since you have passed 23d, which is parsed into 23.0d (a double), there was no error.

Regards,
K.Sivakumar
 
roshni sivan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the above code, I would like to check validity of text entered into a text field of a java AWT application
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may help you.

web page
[ December 20, 2008: Message edited by: Himanshu Gupta ]
 
Campbell Ritchie
Sheriff
Posts: 51453
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do a search; several people have asked about validating text entry like that in the last few weeks.
 
roshni sivan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Himanshu Gupta:
This may help you.

web page

[ December 20, 2008: Message edited by: Himanshu Gupta ]


Thanks. I am doing an AWT application which is to be deployed in a PDA device. Let me check whether I can import the package org.apache.commons.lang.math.NumberUtils to my application.


thanks.

roshni
 
Campbell Ritchie
Sheriff
Posts: 51453
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PDAs? Are you using Java SE or Java ME?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic