Results 1 to 4 of 4

Thread: string to float

  1. #1
    Sungron is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default string to float

    Hi, how can I convert java String to float?
    I tried
    Java Code:
       float f = Float.valueOf(str).floatValue();
    but It gave me exception "NumberFormatException"...
    thanks for any help guys..

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Sungron View Post
    Hi, how can I convert java String to float?
    I tried
    Java 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

  3. #3
    Sungron is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    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...

  4. #4
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    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.

    Java 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 :-)

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •