java - Why am I getting a "missing return statement" error? -
this question has answer here:
this method supposed return true if 1 of first 4 elements in array 9. array length may less 4. in method, reason, keep getting "missing return statement error".
public boolean arrayfront9(int[] nums) { if (nums.length < 4) { int counter = 0; while (counter != nums.length) { if (nums[counter] == 9) { return true; }else{ counter = counter + 1; } } if (counter > nums.length) { return false; } }else{ int counter = 0; while (counter <= 4) { if (nums[counter] == 9) { return true; }else{ counter = counter + 1; } if (counter > 4) { return false; } } } }
`
i understand have make sure no matter code has have return value, given if , else statement, length of array either less 4 or greater or equal 4, no matter array presented should enter 1 of these conditionals?
try one
public boolean arrayfront9(int[] nums) { boolean result = false; if (nums.length < 4) { int counter = 0; while (counter != nums.length) { if (nums[counter] == 9) { result= true; return result; }else{ counter = counter + 1; } } if (counter > nums.length) { result= false; return result; } } else{ int counter = 0; while (counter <= 4) { if (nums[counter] == 9) { result= true; return result; } else{ counter = counter + 1; } if (counter > 4) { result= false; return result; } } } return result; }
actually not returning in method in if/else.
Comments
Post a Comment