I keep getting the
The method add(BigDecimal) in the type BigDecimal is not applicable for the arguments (pay)"
error with the code below.
For reference I have saved the pay class in a separate file, where I import BigDecimal as well.
Would one of you like to point out where I'm lacking/misunderstanding? I've tried to find a solution, but I couldn't find something.
import java.util.Scanner;
import java.math.BigDecimal;
class SalesPreInt {
public static void main(String[] args) {
Pay pay = new Pay();
pay.basePay();
BigDecimal intCalc = new BigDecimal("0.15");
Scanner userInput = new Scanner(System.in);
System.out.println("What were your total sales?");
BigDecimal salesPre = userInput.nextBigDecimal();
System.out.println("You're Total Sales were "+salesPre);
userInput.close();
BigDecimal postIntCalc = salesPre.multiply(intCalc);
BigDecimal salesCom = postIntCalc.add(salesPre);
int finalCalc = salesCom.add(pay);
System.out.println("Your total sales including commission is "+salesCom);
System.out.println("Your total pay is"+finalCalc);
}
}
pay.java file below:
import java.math.BigDecimal;
public class Pay {
public void basePay() {
int basePay = 50000;
BigDecimal bd = new BigDecimal(String.valueOf(basePay));
}
}
Like the error message tells you, the
addmethod ofBigDecimalwith one argument expects aBigDecimalinstance: [javaDoc]You've passed a variable of type
Payto this method and sincePayis not a subtype ofBigDecimalit is not related to it. The methodaddcan't know how to add aPayinstance, the compiler complains about that argument type.You can do the following fix, to bypass that problem:
Your
basePaymethod creates aBigDecimaland I guess this is the one you like to add tosalesCom, so change that method a bit:This method now creates a
BigDecimaland returns it to the calling method. Now change theaddmethod call to use thebasePaymethod:Now there is only one problem left. As you can see in the JavaDoc posted above,
addreturns a newBigDecimalinstance, but you're assigning the returned value to the variablefinalCalc, which is of typeint. So we need to change it toBigDecimal:Now your code compiles and it should work as expected.