Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Converting an integer to a string
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Varsteil




PostPosted: 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
  }
}
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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();
Varsteil




PostPosted: 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.
Insectoid




PostPosted: 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).
Zren




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: