File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Converting a string to a number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting a string to a number" Watch "Converting a string to a number" New topic
Author

Converting a string to a number

Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
Hey,

I have a input dialog box where the user is required to enter a number(any number eg: 11323.9983). I need to take that number and add it to another number. How do i convert the string into any number? eg float, int, long...

I did find a numbers() method but i can't seem to find any syntax examples. any suggestions welcome.

Thanks


Potrzeba jest matka wynalazków
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Go to the API. Click on the java.lang package. You will see some wrapper classes. Click on those, and check what static methods they have.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
Hi,

I looked in the api but it only shows me methods that can convert a string to an int or float or.... unless I'm completely missing it(then please tell me), I'm looking for one method that will convert the string to the necessary number no matter what the user inserts.

Thanks.
subhajit paul
Greenhorn

Joined: Dec 17, 2008
Posts: 17
try xxxxx.

that will work whether the user enters 100.34 or simply 100. it will parse accordingly and give you a double value which you can use for addition.
[ December 17, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
It may be a bit late, but I have deleted your solution. If you look at the top of the Beginner's forum it says
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Simply providing an answer like that deprives the original poster of the opportunity to learn for herself, to find her way round the API documentation etc.

So we prefer not to give straight answers like that; please don't be annoyed with me for deleting it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Originally posted by Maja Gralewska:
Hi,

I looked in the api but it only shows me methods that can convert a string to an int or float or.... unless I'm completely missing it(then please tell me), I'm looking for one method that will convert the string to the necessary number no matter what the user inserts.

Thanks.

What classes have you looked at?
subhajit paul
Greenhorn

Joined: Dec 17, 2008
Posts: 17
Thanks campbell, i have just joined, but i am already loving the warm and friendly learning environment here. And sorry for that, i will definitely refrain myself from the same hereafter....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
, subhajit paul.
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
I have looked at the float, double, integer, number and string classes. In the java.lang package. Am i missing it?

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Except for the fact that they are called Float, Double, Integer, Number and String those are a few to look at. There are also Byte, Short and Long. Boolean and Character are also available for wrapping boolean and char to objects.

Inside Integer, what static methods have you found? Does one of those look like it can return an int? Now once you've found that one, Float, Double, Byte, Short and Long have similar methods.
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
I have found 3, Integer(String s), Decode(String s) & parseInt(String s) and similar ones in the other classes, but is there only One method that will convert the string to the required number no matter what it is? eg: 123342.34234 or 100

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
You mean telling whether the String represents an int or a double before parsing? Sounds like a regular expression with . in the middle. There is lots in the Java Tutorials.

Beware "." is a meta-character which must be escaped. You can find the format for floating point number in the "grammar" in the Java Language Specification.
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
Thanks, what you suggested works perfectly, only now when it is converted to a double, and i try to add it to an int it concatenates it instead!

Here is my code



Thanks
subhajit paul
Greenhorn

Joined: Dec 17, 2008
Posts: 17
you are using the right method, but didn't you throw away the result of parsedouble?

remember,in java,nothing is passed by
reference
,so you can never expect that reply will come back modified.

moreover you are using
reply is a string and '+' operator is used by strings to concatenate
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
Thanks Paul i see my mistake
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting a string to a number