Article View: borland.public.jbuilder.java.language
Article #18785Re: String Conversions
From: Clint Brewer
Date: Tue, 28 Apr 1998 00:00
Date: Tue, 28 Apr 1998 00:00
63 lines
2873 bytes
2873 bytes
Julio Rincon wrote: > > I'm trying to do simple calculations on a frame and I'm having many > problems. To simplify I have setup two FieldControl components on a > Frame (fieldOne and fieldTwo) and I am trying to do the following: > > 1) pull the string from fieldOne > 2) convert it to integer to do some calculations > 3) convert the new value back to string > 4) set the textfield of fieldTwo to new string value > > I setup an event with the following code that would execute if the > value of fieldOne changed, the calculations are omitted since they're > not the cause of the problem: > > String test = fieldOne.getText(); > int itest = Integer.parseInt(test); > String stest = Integer.toString(itest); > fieldTwo.setText(stest); > > This works if I type integers into fieldOne but it doesn't work if I > type in currency amounts (e.g. 55.55). When I type in currency > amounts nothing happens, not even an error message. > > If I link fieldOne to a table field in a database I get an error > message: EventDispatchThread.java:63. > > I have tried converting the text from fieldOne to a float number, but > not have much luck since I can't figure out the correct syntax from > the Float class (valueOf). I have been trying to figure this out for > a couple of hours and am quite confused. > > Any help would be appreciated! > > Julio. Hello Julio, Yeah, I got mixed up on this in the past when i was trying to do a lot of conversions as well. I found that the easiest way to do the conversion from string to a float or int was to use the Integer class or the Float class. The constructor for these classes takes a string as an argument if you want, and then converts it into a int or float. For instance you could do something like.. Integer Field1_int=new Integer(fieldOne.getText()); then, the Integer class has plenty of utility functions to play with that integer. As far as the currency, 55.55 is a float and shouldn't be used with an int. If you pass "55.55" to the Integer constructor, I believe it should throw a NumberFormatException. You could then catch that and tell the user to enter an int and not a float, or something along those lines. hope this helps, Clint Brewer
Technical Details
Message-ID:
<354677ED.4D71@borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3544318b.861070400@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,896ae2f6a10aacdf X-Google-Attributes: gidf74b3,public From: Clint Brewer <supportinfo@borland.com> Subject: Re: String Conversions Date: 1998/04/28 Message-ID: <354677ED.4D71@borland.com> X-Deja-AN: 348588273 Content-Transfer-Encoding: 7bit References: <3544318b.861070400@forums.borland.com> Mime-Version: 1.0 To: Julio Rincon <julioCR@aol.com> Content-Type: text/plain; charset=us-ascii Organization: Borland International Newsgroups: borland.public.jbuilder.java.language