This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Issue with Float.parseFloat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Issue with Float.parseFloat" Watch "Issue with Float.parseFloat" New topic
Author

Issue with Float.parseFloat

roshni sivan
Greenhorn

Joined: Nov 23, 2008
Posts: 20
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

Joined: May 29, 2008
Posts: 3
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

Joined: Nov 23, 2008
Posts: 20
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

Joined: Aug 18, 2008
Posts: 598

This may help you.

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

My Blog SCJP 5 SCWCD 5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Please do a search; several people have asked about validating text entry like that in the last few weeks.
roshni sivan
Greenhorn

Joined: Nov 23, 2008
Posts: 20
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

Joined: Oct 13, 2005
Posts: 39478
    
  28
PDAs? Are you using Java SE or Java ME?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with Float.parseFloat