Computer Science Canada Converting an integer to a string |
Author: | Varsteil [ Mon Nov 05, 2012 10:29 am ] | ||
Post subject: | Converting an integer to a string | ||
I'm working on a program that lists coins as integers, then converts them to their values in dollars as a string, for example, 1 quarter and 1 dime (the inputs being 1 and 1) outputs as a string "$0.35"
|
Author: | Insectoid [ Mon Nov 05, 2012 10:46 am ] | ||
Post subject: | RE:Converting an integer to a string | ||
You need to convert the sum integer to a string. Add them all up inside a pair of parentheses, and call toString() on the result of that.
|
Author: | Varsteil [ Mon Nov 05, 2012 10:48 am ] |
Post subject: | Re: Converting an integer to a string |
I'm getting an "illegal start of expression" error, also, apparently, I'm only supposed to use 4 integers, so I don't think I can have another integer for the sum. |
Author: | Insectoid [ Mon Nov 05, 2012 11:02 am ] |
Post subject: | RE:Converting an integer to a string |
Oh, woops. total needs to be declared static. Also you have an extra '+' in that last line. Forget everything I said in my first post. EDIT: Also, I recommend you multiply all your coin values by 100 when you add them (ie 1 penny = 1, not 0.01) and then divide the total by 100, or you'll get floating point errors (which basically means half your answers will be off by one penny). |
Author: | Zren [ Mon Nov 05, 2012 4:40 pm ] |
Post subject: | Re: Converting an integer to a string |
The static methods Integer.toString(int), Double.toString(double), etc is used to apply the Boxed/Wrapper like methods that come with the Object version of the data type on the primitive data types. Eg: Integer.toString(14); Which might be what Insectoid was getting at if the wrapping ().toString() auto-boxes the value inside the brackets. However, you should learn to recognize when it is getting boxed into an Object and avoid such if at all possible. I kinda just realized you might not have any idea about Objects and Autoboxing... Meh. |