Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting a string to a number

 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
, subhajit paul.
 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul i see my mistake
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic