You have no guaranteed return statement in doMath(). If op falls through all of the cases in your switch, the method would not return a value. Add a default return value after the switch and it will be fine
static int doMath(char op,int numa, int numb){
switch (op)
{
case '+':
return numa + numb;
case '-':
return numa - numb;
case '*':
return numa * numb;
case '/':
return numa / numb;
}
return 0; // or another default if you prefer
}
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Or better yet, write it to have a single exit point...
static int doMath(char op,int numa, int numb) {
int result;
switch (op) {
case '+':
result = numa + numb;
break;
case '-':
result = numa - numb;
break;
case '*':
result = numa * numb;
break;
case '/':
result = numa / numb;
break;
default:
result = 0;
}
return result;
}
though I'd probably not return 0 on an invalid operator but throw an IllegalArgumentException.
Returning 0 WILL lead to hard to find bugs later, as 0 is a valid return value for any of the valid operations as well.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337