The Artima Developer Community
Sponsored Link

Java Answers Forum
how do you convert a String to an int like this?

2 replies on 1 page. Most recent reply: Mar 23, 2004 10:24 AM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Mickey

Posts: 7
Nickname: internment
Registered: Feb, 2004

how do you convert a String to an int like this? Posted: Mar 23, 2004 8:12 AM
Reply to this message Reply
Advertisement
Hi folks,
I'm trying to convert a string which has a decimal number in it like so 19.5! What I'm trying to do is convert this into an int like so and then I'm doing a little conditional test on it:

int currentTempNumber;
currentTempNumber = Integer.parseInt(this.currentTemp.trim());

this.currentTemp is a string holding the 19.5. I get the following error:

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

Is this not possible to do...!???! If not then can I convert the String to a float...!???! The this.currentTemp needs to be a String for another part of program to work in case you are wondering. Any help is appreciated. Thanks!


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: how do you convert a String to an int like this? Posted: Mar 23, 2004 8:50 AM
Reply to this message Reply
Like u did Integer.parseInt there are methods like

Float.parseFloat(String s)
and Double.parseDouble to parse Strings

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: how do you convert a String to an int like this? Posted: Mar 23, 2004 10:24 AM
Reply to this message Reply
java.lang.NumberFormatException.forInputString(NumberFormat
Exception.java:48)

> Is this not possible to do...!???!

You can't use the parseInt method to parse something that isn't an integer.

>If not then can I
> convert the String to a float...!???!

Yes, as mausom pointed out, the Float and Double classes each have parsing methods that are similar, but return floating point values. Double is the more common choice, but it really doesn't matter.

After you parse to the float or double, it is fairly easy to convert to an int. But first you have to decide what to do about the fractional portion. If you just want to eliminate it, you can simply use a typecast.
int myInt = (int) myDouble;

But if you want to round the value, go into the Math class. It has a couple of rounding methods that you can choose from. I'm working from memory, but I think one is called rint(double) and that it does the traditional rounding to the nearest integer like we learned in grade school. Check the documentation. If you haven't downloaded it yet, you can view it online at
http://java.sun.com/j2se/1.4.2/docs/api/index.html

>The
> this.currentTemp needs to be a String for another part of
> program to work in case you are wondering. Any help is
> appreciated. Thanks!

Parsing Strings to get numeric values is pretty common. That is why there are already methods in place to handle the process. None of this will affect your original String.

Flat View: This topic has 2 replies on 1 page
Topic: error? Previous Topic   Next Topic Topic: Who Called Me?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use