I am getting a incompatible types required int found:void on this line
–> moneyCount = countDemoninations(change, denomination[i]). trying to understand why?
public class Change {
int [] denomination = {1,2,5,10,20,50,100,200,500,1000,2000,5000};
int moneyCount = 0;
public void catagorizeChange(int change){
for (int i = 0; i < denomination.length; i++){
moneyCount = countDemoninations(change, denomination[i]);
}
}
public void countDemoninations(int change, int denomination){
int moneyCount =0;
while (change >= denomination){
moneyCount = moneyCount++;
change = change - denomination;
}
}
}
I’m a new java student, I want to know if the following edited code below in a good practice i.e have one method used in another where both belong to the same class?
public void countChangedenominations (int change){
for (int i = 0; i < moneyValuearray.length; i++){
moneyCountarray[i] = countDemonination(change, moneyValuearray[i]);
}
}
public int countDemonination(int change, int denomination){
while (change >= denomination){
moneyCount = ++moneyCount;
change = change - denomination;
}
return moneyCount;
The source of the problem with the line:
is that
countDemoninationsis of type void, whereasmoneyCountis of typeint.To solve the issue, change your
countDemoninationsmethod signature to returnintinstead ofvoid, then returnmoneyCountas the last statement in your method: