Question

I'm new here(5 days old to be exact) making basic programs in Android and Java. Here's the code I'm working on and I'm stuck at the 'else if' on sendAmount() part of this code.

I got an error there that says "The operator ^ is undefined for the argument type(s) double, double".

Now my question is, how can I make it work and display the result?

private EditText entPrinAmt;
private EditText entIntRate;
private EditText entTol;
private EditText entMonAmt;

private TextView txtFactor;
private TextView txtFutValue;
private TextView txtIntIncome;
private TextView txtEffRate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    entPrinAmt = (EditText) findViewById(R.id.ent_prin_amt);
    entIntRate = (EditText) findViewById(R.id.ent_int_rate);
    entTol = (EditText) findViewById(R.id.ent_tol);
    entMonAmt = (EditText) findViewById(R.id.ent_mon_amt);

    txtFactor = (TextView) findViewById(R.id.txtFactor);
    txtFutValue = (TextView) findViewById(R.id.txtFutureVal);
    txtIntIncome = (TextView) findViewById(R.id.txtIntIncome);
    txtEffRate = (TextView) findViewById(R.id.txtEffRate);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void sendAmount(View view) {

    DecimalFormat df = new DecimalFormat("#,###,###,###,###,###.##");
    DecimalFormat factorFormat = new DecimalFormat("#,###.#####################");

    double dPA = Double.parseDouble(entPrinAmt.getText().toString());
    double dIR = Double.parseDouble(entIntRate.getText().toString());
    double dTL = Double.parseDouble(entTol.getText().toString());
    double dMA = Double.parseDouble(entMonAmt.getText().toString());

    if (dPA < 0){
        Context context = getApplicationContext();
        CharSequence txtToast = "Must not be negative!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, txtToast, duration);
        toast.show();
    }
    else if (dPA == 0 && dIR>0 && dTL>0 && dMA>0){
        Double resultPA;
        resultPA = ((dMA * ((1+dIR/12)^dTL-1)/((1+dIR/12)^dTL*dIR/12)),2);

        /*String f = df.format(resultPA);  <-v-DO NOT MIND THIS
        txtFactor.setText("Principal Amount: P" + f);*/
    }
    else {
        Context context = getApplicationContext();
        CharSequence txtToast = "Can't Compute Principal Amount!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, txtToast, duration);
        toast.show();
    }
}

BTW, I got that equation from here:

return ROUNDOFF((nPmt*((1+nRate/12)^nPer-1) / ((1+nRate/12)^nPer*nRate/12)),2)

I think this is from C or C++ platform. I don't understand the ',2' part tho. How can I apply that to the class I'm working on? Any help will be appreciated.

Was it helpful?

Solution

^ operator is bitwise xor, not power. In fact, Java has no power operator defined. You should use Math.pow instead.

Additionally, you should use BigDecimal when working with currencies, for a number of reasons you will find explained here on SO.

OTHER TIPS

Yes, in java The operator ^ (bitwise xor) is undefined for the argument type(s) double, double. Use Math.pow(double , double) instead of ^.

So your code will look like this:

resultPA = ((dMA * (Math.pow((1+dIR/12),dTL-1))/(Math.pow((1+dIR/12),dTL)*dIR/12)),2);

^ is a bitwise XOR operation, most likely you want to

double d = Math.pow(a, b); // a^^b 

Use pow function.

double output = Math.pow(x,y);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top