Results 1 to 5 of 5
  1. #1
    latereg is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Need help on this one part of my program

    import java.util.Scanner;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    public class Moneychanger
    {
    public static void main (String[] args)
    {
    Scanner scan = new Scanner (System.in);
    DecimalFormat formatting = new DecimalFormat("0.00");
    System.out.print("Enter price of an item, between $0.05 and $10.00, and in 5-cent increments: $");
    double price = scan.nextDouble();
    System.out.print("Enter amount paid: $");
    double payment = scan.nextDouble();
    double change = payment - price;
    System.out.println("The amount of change you will receive is: $" + formatting.format(change));
    double cents = change * 100;
    double value = (Math.ceil(cents));
    value = (int)cents;
    double fives = value / 500;
    System.out.println("$5 bills: " + (NumberFormat.format(fives)));
    double ones = ((value) - (5 * fives));

    and it says "non-static method format (double) cannot be referenced from a static context"

    did i do the numberformat right?
    and how do i correct this?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.text.*;
    
    public class Test {
        public static void main(String[] args) {
            NumberFormat nf = NumberFormat.getInstance();
            nf.setMinimumFractionDigits(2);
            nf.setMaximumFractionDigits(2);
            NumberFormat df = new DecimalFormat("0.00");
            NumberFormat cf = NumberFormat.getCurrencyInstance();
            double amt = 1000.0/3;
            System.out.println("nf: " + nf.format(amt));
            System.out.println("df: " + df.format(amt));
            System.out.println("cf: " + cf.format(amt));
            System.out.printf("pf: %.2f", amt);
        }
    }

  3. #3
    latereg is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    thanks, but i was really tryiin to figure out how to use the format method under NumberFormat cause thats what ive been told to do.....could u help me with that, sorry for being so picky

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The compiler is telling you that the NumberFormat class does not have a static method format.
    Use your local/instance formatter as shown in the example of post #2.
    That is, in your code, replace this
    Java Code:
    System.out.println("$5 bills: " + (NumberFormat.format(fives)));
    with this
    Java Code:
    System.out.println("$5 bills: " + formatting.format(fives));

  5. #5
    latereg is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Need help ASAP with Payroll Program Part 2
    By arrech326 in forum New To Java
    Replies: 10
    Last Post: 11-17-2009, 11:17 PM
  2. Inventory Program Part 2 of 6
    By tlouvierre in forum New To Java
    Replies: 2
    Last Post: 05-28-2009, 02:30 AM
  3. Part of program not printing ?
    By graz in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 04:37 AM
  4. Inventory Program Part 3 ~ please help!
    By marMcD in forum New To Java
    Replies: 13
    Last Post: 02-25-2009, 06:57 AM
  5. Java Inventory Program Part 3
    By ljk8950 in forum New To Java
    Replies: 18
    Last Post: 07-28-2008, 06:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •