• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

float methods

 
Jacob Steingart
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stuart Smith
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.).
 
Jacob Steingart
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I think I get it. Thanks!
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jacob Steingart:
Okay, I think I get it. Thanks!
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic