aspose file tools*
The moose likes Beginning Java and the fly likes How to convert a string to a float or double? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to convert a string to a float or double?" Watch "How to convert a string to a float or double?" New topic
Author

How to convert a string to a float or double?

Suresh Ray
Ranch Hand

Joined: Feb 21, 2000
Posts: 55
This part of the code captures input and converts string values to relavent primitive types,using Wrapper classes.
Here is the code:
String name = JOptionPane.showInputDialog( null,
"Enter your name here : " );
String inputint = JOptionPane.showInputDialog( null,
"Enter your age here : " );
int age = Integer.parseInt( inputint );
String inputsal = JOptionPane.showInputDialog( null,
"Enter your salary here : " );
float sal = Float.parseFloat( inputsal );
When I compile it gives error at last line which parses float from the string. Why can't we use the parseFloat similar to parseInt?
Thanks..
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I don't get any error using jdk 1.3 beta on Windows 98. What error exactly are you getting?


"I'm not back." - Bill Harding, Twister
Suresh Ray
Ranch Hand

Joined: Feb 21, 2000
Posts: 55
I am working on an IDE, JDeveloper, which uses JDK 1.2, and it gives an error as :
method parseFloat(java.lang.String)not found in class java.lang.Float
Thandapani Saravanan
Ranch Hand

Joined: Oct 17, 1999
Posts: 117
I don't get any error in JDK 1.2.2 compiler.


Saravanan
Chris Pocock
Greenhorn

Joined: Mar 11, 2002
Posts: 13
I get an error when I try the same thing. Do you have to import a special package for these? If its just my JDK, where can I download the lastest one?
Thanks
-Chris


<i>I'm currently reading 'How to program Java' by Deitel</i><br /><b>I am still learning, so be kind. I also know C++, by the way</b>
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
java.sun.com.
Here's a link to the latest jdk:
http://64.124.140.247/Download5


Rob
SCJP 1.4
Chris Pocock
Greenhorn

Joined: Mar 11, 2002
Posts: 13
Thanks
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"face_master",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Thanks.
Dave
ps: really, please read the Naming standards, you didn't even come close!
Chris Pocock
Greenhorn

Joined: Mar 11, 2002
Posts: 13
I downloaded the JDK 1.3 from that link and set my compiler (RealJ) to know where it is on my computer, but I still get the following errors:

The problem is obviously in these two strings:

Does anybody know what i'm doing wrong?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
method names always start with a lowercase letter.
Look at the JavaDoc for the API libraries. It documents all the methods in all the classes.
the method you want is parseInt(), not ParseInt().
Java is case-sensitive.
Chris Pocock
Greenhorn

Joined: Mar 11, 2002
Posts: 13
Whoa sorry. I'm a C++ programmer so I guess its a force of habit...
Chris Pocock
Greenhorn

Joined: Mar 11, 2002
Posts: 13
Hand on...I just tried to recompile then I got another error (GRR!). Here it is
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
Two excellent sources of information are The Java 2 Platform, Standard Edition, v 1.4.0 API Specification and The Java Language Specification.
Looking at the String class documentation in the API Specification, we find that no method parseString exists. Take a look at the documentation to see what methods are available and how they work.
Good Luck,
-Dirk Schreckmann
[ March 11, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Another great source to assist along the path of learning Java is Sun's Java Tutorial.
----
Hint: Perhaps what you wanted to do before was use one of the String.valueOf methods.
Good Luck,
-Dirk Schreckmann
[ March 11, 2002: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Technically speaking, at what point was this thread hijacked? Was it before or after this post?
-Dirk Schreckmann
Newest member of Hijackers Anonymous
[ March 11, 2002: Message edited by: Dirk Schreckmann ]
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
This thread wasn't hijacked at all. He was having small problems that looking at a tutorial and/or the API would solve. That is until you brought up hijacking threads.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Thanks for the clarification Paul. I was curious as to just how sensitive the Police were to hijacking. Your position seems perfectly reasonable.
[ March 11, 2002: Message edited by: Dirk Schreckmann ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Chris Pocock:
Hand on...I just tried to recompile then I got another error (GRR!). Here it is

If you look up the API documentation for String, you'll see there is no such method in the String class called "parseString()";
If you're trying to create a String object from an int value, you want to use String.valueOf(int value);
If you want to parse a String value and return an int, you would use Integer.parseString(String intString);
Again, you need to become familiar with the JavaDoc (documentation for the java API).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert a string to a float or double?