Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-02-2010, 08:59 AM
Member
 
Join Date: Jan 2010
Posts: 10
Rep Power: 0
Sungron is on a distinguished road
Default string to float
Hi, how can I convert java String to float?
I tried
Code:
   float f = Float.valueOf(str).floatValue();
but It gave me exception "NumberFormatException"...
thanks for any help guys..
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-02-2010, 09:07 AM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,219
Rep Power: 3
JosAH is on a distinguished road
Default
Originally Posted by Sungron View Post
Hi, how can I convert java String to float?
I tried
Code:
   float f = Float.valueOf(str).floatValue();
but It gave me exception "NumberFormatException"...
thanks for any help guys..
That method works (also have a look at the parseFloat( ... ) method) but your String str didn't contain a valid floating point number representation.

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-02-2010, 09:37 AM
Member
 
Join Date: Jan 2010
Posts: 10
Rep Power: 0
Sungron is on a distinguished road
Default
Oh, damn how silly is me....now I see, just too much C, I expected it to return 0...
hmm so how can I verify is string really a float? Such exceptions are annoying since they seem to stop whole function...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-02-2010, 10:23 AM
Member
 
Join Date: Nov 2007
Location: New Zealand
Posts: 36
Rep Power: 0
Turtle is on a distinguished road
Default
Exceptions don't need to stop a function ("method" in java terminology).
Most exceptions can be recovered from.
Infact, they let you write cleaner code because you aren't inserting little checks and thus the purpose of a code block is clearer. Instead you can wrap problem code in a "try catch" block and handle the exceptions at the end.

I have included a code sniplet the demonstrates forcing the user to input a valid float. If the float is invalid, the user is hassled until they do enter a correct value.

Code:
import java.io.Console;

class TestFloat {
	
	public static void main(String[] args) {
		
		// Get console object to read from console...
		Console cons;
		if ((cons = System.console()) == null){
			System.out.println("Can't access console, closing program");
			System.exit(1);
		}
		
		// Prompt user for a valid float, repeat until a valid float is entered
		String line;
		float f;
		while(true) {
			// Prompt user for float
			System.out.print("Please enter a valid float: ");
			// Read in user input as string
			line = cons.readLine();
			
			// Wrap conversion code in a try catch block to handle the exception
			try {
				// Try convert String to float (may throw exception)
				f = Float.valueOf(line);
				// Exit while loop if an exception is not thrown
				break;
			} catch(NumberFormatException e) {
				// An exception was thrown, inform user and repeat loop
				System.out.println("That is not a valid float");
			}
		}
		
		// A valid float value was (eventually entered), display it
		System.out.println("Float value was: " + f);
		
	}
}
Check out: Lesson: Exceptions (The Java™ Tutorials > Essential Classes) for details on exceptions.

Hope this helps :-)
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Working with float dardar New To Java 8 01-27-2010 09:29 PM
Float vs. Double javanub New To Java 1 11-23-2008 01:11 PM
String to Float durahman New To Java 2 02-12-2008 01:17 AM
Float to String mew New To Java 4 12-29-2007 06:08 PM
convert string to float miss_dot NetBeans 1 11-15-2007 12:26 AM


All times are GMT +2. The time now is 07:40 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org