1

I don't understand why I am getting an error of an incompatible data type, I am using a float for a financial value, I do not want to go past two decimal places!

I was under the impression that you could do this with a float, however I am getting an error returned to me saying,

Constructor Magazine cannot be applied to given types.

When I just make the float 7 as opposed to 7.99, it works fine!

Have I misunderstood what a float is, do I need to use a double instead?

I'll just show my magazine class and a bit of my test class to demonstrate.

Test class:

The following is a segment from my test class trying to use a float to two decimal places:

public static void main()
{
    Magazine magazine1 = new Magazine("SanYonic Publishing", "Ayup Magazine", 7.99, "Yeshumenku Suni", "12/09/2011");

    System.out.println();
    magazine1.getEditor();
    magazine1.getDate();
    magazine1.getPublisher();
    magazine1.getPublicationTitle();
    magazine1.getPrice();
    System.out.println();
    …
}

Magazine class:

/**
 * Magazine Class - This class represents Magazine Objects
 */
public class Magazine extends Publication
{

    private String editor;
    private String date;

    public Magazine(String publisherIn , String publicationTitleIn, float priceIn, String editorIn, String dateIn)
    {
        super (publisherIn , publicationTitleIn, priceIn);

        editor = editorIn;
        date = dateIn;
    }

    public void setPublication(String publisherIn, String publicationTitleIn, float priceIn)
    {
        publisherIn = publisher;
        publicationTitleIn = publicationTitle;
        priceIn = price;
    }

    public String getEditor()
    {
        System.out.println("The editor of this magazine is " + editor);
        return (editor);
    }

    public String getDate()
    {
        System.out.println("The publication date of this magazine is " + date);
        return (date);
    }

    public String getPublisher()
    {
        System.out.println("The publisher of this magazine is " + publisher);
        return (publisher);
    }

    public String getPublicationTitle()
    {
        System.out.println("The publication title of this magazine is " + publicationTitle);
        return (publicationTitle);
    }

    public float getPrice()
    {
        System.out.println("The price of this magazine is £" + price);
        return (price);
    }
}
Phish
  • 774
  • 3
  • 11
  • 27
Phil
  • 1,393
  • 5
  • 23
  • 42
  • 4
    I'd recommend that you do not use `float` (or `double`) for financial calculations. Floating point numbers are not precise and you can get strange rounding errors. It's usually much better to use integer or fixed point arithmetic, i.e. compute everything in terms of an integer number of cents or use the `BigDecimal` class. – Cameron Skinner Jan 18 '12 at 21:38
  • 1
    I would strongly suggest you use BigDecimal instead of float/double as the primitives types have rounding issues storing a large sub-set of decimal values. – Mike Q Jan 18 '12 at 21:38

2 Answers2

3

you need

Magazine magazine1 = new Magazine ("SanYonic Publishing", "Ayup Magazine", 7.99f, "Yeshumenku Suni", "12/09/2011");

note the 7.99f to fix the compile issue.

Note that both floats and doubles are not suitable for monetary calculations (if you care about accuracy), since they can only represent a discrete set of values. All currency calculations should be done with BigDecimal.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
3

In Java, a number with a decimal point in it is a double by default. Try 7.99f.

Also, if you're doing calculations with currency, you should take a look at BigDecimal to avoid weird rounding errors later down the road.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • Thanks Jeffery, I am trying to implement Bigdecimal as I have not beofre but struggling, I have imported java.math, java.lang.Object and java.lang.Number, I have then put BigDecimal instead of float when defining my price variable but not sure how to define my number in my test. – Phil Jan 18 '12 at 22:15