bigdecimal - Error With Big Decimal in Java -
i keep getting
the method add(bigdecimal) in type bigdecimal not applicable arguments (pay)"
error code below.
for reference have saved pay class in separate file, import bigdecimal well.
would 1 of point out i'm lacking/misunderstanding? i've tried find solution, 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 total sales?"); bigdecimal salespre = userinput.nextbigdecimal(); system.out.println("you're total sales "+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 "+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 error message tells you, add method of bigdecimal 1 argument expects bigdecimal instance: [javadoc]
public bigdecimal add(bigdecimal augend)
returns bigdecimal value (this + augend), , scale max(this.scale(), augend.scale()).
parameters:
augend - value added bigdecimal.returns:
this + augend
you've passed variable of type pay method , since pay not subtype of bigdecimal not related it. method add can't know how add pay instance, compiler complains argument type.
you can following fix, bypass problem:
your basepay method creates bigdecimal , guess 1 add salescom, change method bit:
public bigdecimal basepay() { int basepay = 50000; return new bigdecimal(string.valueof(basepay)); } this method creates bigdecimal , returns calling method. change add method call use basepay method:
int finalcalc = salescom.add(pay.basepay()); now there 1 problem left. can see in javadoc posted above, add returns new bigdecimal instance, you're assigning returned value variable finalcalc, of type int. need change bigdecimal:
bigdecimal finalcalc = salescom.add(pay.basepay()); now code compiles , should work expected.
Comments
Post a Comment