Bigdecimal error cannot find Symbol

Asked

Viewed 190 times

1

Given an amount in R$ convert it into dollar.

Well, based on this context, the teacher requested that we use the object orientation paradigm. Before I should clarify that I use to order ide Bluej to solve the problem, according to my teacher he is better for didactic purposes, I created a class Cambio and a class Main, that is leased the methods that receives the amount, the exchange rate and the conversion, this initializes system so to speak contained the method main.

exchange class:

public class Cambio
{
    private float amount;
    private double conversion;
    private float exchange;

    /**
     * Construtor
     */
    public Cambio(float amount, float exchange)
    {
        setAmount(amount);
        setExchange(exchange);
        conversion = 0;
    }

    /**
     * Método modificador: quantia.
     */
    public void setAmount(float amount)
    {
        this.amount = amount;
    }

    /**
     * Método modificador: taxa.
     */
    public void setExchange(float exchange)
    {
        this.exchange = exchange;
    }

    /**
     * Metodo acessador: quantia
     */
    public float getAmount()
    {
        return this.amount;
    }

    /**
     * Metodo acessador: taxa
     */
    public float getExchange()
    {
        return this.exchange;
    }

    /**
     * Metodo modificador: calcula o cambio.
     */
    public double getConversion()
    {
        BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);

        conversion = (float)this.amount / (float)this.exchange;

        return df.doubleValue(conversion);
    }
}

Main class:

public class Main
{
    public static void main(String[] args)
    {
        Scanner read = new Scanner(System.in);

        System.out.print("Digite a quantia: ");
        float amount = read.nextFloat();

        System.out.print("Digite a taxa cambial: ");
        float exchange = read.nextFloat();

        Cambio cb = new Cambio(amount, exchange);

        System.out.println("A valor em dolar: USS$" + cb.getConversion());
    }
}

I am using the class to format the values BigDecimal. Make a mistake and don’t compile:

cannot find Symbol (Bigdecimal class)

On the line:

BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);

As far as I understand reading the Bluej documentation I’m calling an object that doesn’t exist. I’ve used the BigDecimal to format values in double in another exercise.

  • There is nothing else in your code? Which line gives the error? Show the line, no use speaking the number.

  • Not all code is this, the line that gives error is 62 (Bigdecimal db = new Bigdecimal(d). setScale(2, Roundingmode.HALF_EVEN))

1 answer

1


This code uses a variable called d on the line

BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);

this variable is not declared anywhere in the code. Surely this causes the problem. You have to see what you want to use there. Whether the method should receive a parameter or pick up a class member.

On the line

df.doubleValue(conversion);

has another undeclared variable in use. Maybe in this case it was to be bd in place of df.

I put in the Github for future reference.

Work with float and double for monetary value does not work. But this is another problem.

Playing random codes doesn’t work either. The code needs to make some sense, it needs to have a minimum of coherence with what exists.

  • Good according to documentation did not need but, I imported yes when gave the first error, I used the way you mentioned, really I got confused in df ( by db) but, in the code had already fixed this... regarding the variable d could explain me a little more, according to the tutorial I read on the internet it mentions this line this way.

  • 1

    Yes, in the context of this tutorial, now you are doing something else. You have to figure out what you want to use as decimal number. It would be the amount? It’s what I said there’s no point in copying and pasting a code you found. It’s not programming. You have to write each piece of code with some sense, you have to know why each part is there. Without knowing this, nothing will work. You probably need to study programming in the general sense before you start coding. Programming is not copying cake recipe. And I’d love to know the documentation that says you don’t have to import anything necessary.

  • Referring to documentation, it’s from Bluej himself - in fact I’m still learning to program. Without leaving the merit of the question, I built based on my knowledge, I did not like the way it was being printed and I sought more knowledge, in order to improve my algorithm, well which I would like to format is the variable "Conversion" I edited the question, I failed to enter the code in the question

  • Anyway, let to receive this instruction to manipulate data or format data when teachers think it is necessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.