This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes float methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "float methods" Watch "float methods" New topic
Author

float methods

Jacob Steingart
Ranch Hand

Joined: Mar 28, 2008
Posts: 63
I know from Integer.parseInt() that primitive variable types have methods, but do other variable types have methods as well? I want something similar to the parseInt() method, but for a float. I'm made a program that will solve any linear equation, but it needs to change the user generated String into a float in order to do something like y = .5x + b. If anyone knows a method off the top of their heads, please share


I never know what to put in my signature...
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
parseFloat(String s)

Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.
[ March 29, 2008: Message edited by: Stuart Smith ]

Dale Carnegie:<br />"Most of the important things in the world have been accomplished<br />by people who have kept on trying when there seemed to be no hope at all."
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Bookmark the API documentation for reference.

Note that each of the primitive types has a corresponding "wrapper" class (Integer for int, Character for char, etc.).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jacob Steingart
Ranch Hand

Joined: Mar 28, 2008
Posts: 63
Thank you very much. That can be applied to a readLine() method as well, right? So if your keyboard was called kb, you could say float g = parseFloat(kb.readLine());
Is that how that would work? Or would you do it like a parseInt() and say:
float g = Float parseFloat(kb.readLine());
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Originally posted by Jacob Stein****:
I know from Integer.parseInt() that primitive variable types have methods.
No they don't. You cannot writeYou would have to wrap the int na an Integer object and then call the methods on that Integer object.

Same with Byte Boolean Character (for char) Float Double Long and Short.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Originally posted by Jacob Steingart:
Thank you very much. That can be applied to a readLine() method as well, right? So if your keyboard was called kb, you could say float g = parseFloat(kb.readLine());
Is that how that would work? Or would you do it like a parseInt() and say:
float g = Float parseFloat(kb.readLine());
You would have to have a String whivh represents a float in a single token (String s) and call

Float f = Float.parseFloat(s);

Easier to use the nextFloat() method of the java.util.Scanner class.
Jacob Steingart
Ranch Hand

Joined: Mar 28, 2008
Posts: 63
Okay, I think I get it. Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Originally posted by Jacob Steingart:
Okay, I think I get it. Thanks!
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: float methods
 
Similar Threads
float error
casting: float to Float
passing a float to a double
passing a float to a double
Format for a Method