Converting an integer to a string
Author |
Message |
Varsteil
![](http://compsci.ca/v3/uploads/user_avatars/15841261014e8660898a30b.jpg)
|
Posted: 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"
code: | import java.util.Scanner;
public class AddCoins {
static int penny, nickel, dime, quarter;
String total;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the amount of pennies you have");
penny = input.nextInt();
System.out.println("Enter the amount of nickels you have");
nickel = input.nextInt();
System.out.println("Enter the amount of dimes you have");
dime = input.nextInt();
System.out.println("Enter the amount of quarters you have");
quarter = input.nextInt();
total = "Total: $" + (penny * 0.01) + (nickel * 0.05) + (dime * 0.1) + (quarter + * 0.25); //Illegal start of type error
}
} |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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.
code: | total = "Total: $" + ((penny * 0.01) + (nickel * 0.05) + (dime * 0.1) + (quarter + * 0.25)).toString(); |
|
|
|
|
|
![](images/spacer.gif) |
Varsteil
![](http://compsci.ca/v3/uploads/user_avatars/15841261014e8660898a30b.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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). |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
|
|