# largest number of the range

Hi,

I am trying below challenge
http://codingbat.com/prob/p177372

I wrote code as below

``````public int max1020(int a, int b) {

If(a>=10 && a<=20 && b>=10 && b<=20)
{
if(Math.abs(10-a)>Math.abs(10-b))
{
return b;
}
return a;
}
return 0;

}
``````

I am getting error as below

Compile problems:

Error:      If(a>=10 && a<=20 && b>=10 && b<=20)
^
Syntax error, insert ";" to complete BlockStatements

How to fix this error and improve above code. I really do not want return 0. I want to remove that line
But i cannot remove that line as the method is expecting return just before method closing braket(parenthesis)

LVL 7
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
``````public int max1020(int a, int b) {

if(Math.abs(10-a)>Math.abs(10-b))
{
return b;
}
return a;
}
``````

when i write like above failing test cases as i am not checking if numbers in range of 10,20

Expected      Run
max1020(11, 19) → 19      11      X
max1020(19, 11) → 19      11      X
max1020(11, 9) → 11      11      OK
max1020(9, 21) → 0      9      X
max1020(10, 21) → 10      10      OK
max1020(21, 10) → 10      10      OK
max1020(9, 11) → 11      9      X
max1020(23, 10) → 10      10      OK
max1020(20, 10) → 20      10      X
max1020(7, 20) → 20      7      X
max1020(17, 16) → 17      16      X
Author Commented:
when i put the condition check for 10,20 range i am getting errors as posted in my original question. please advise
Commented:
You misspelled 'if' as 'If'

change the 'I' to 'i' and java should be able to compile it.

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Good catch. I wonder why i could not noticed it.

``````public int max1020(int a, int b) {

if(a>=10 && a<=20 && b>=10 && b<=20)
{
if(Math.abs(10-a)>Math.abs(10-b))
{
return b;
}
return a;
}
return 0;

}
``````

i still have failed test cases which i need to fix

Expected      Run
max1020(11, 19) → 19      11      X
max1020(19, 11) → 19      11      X
max1020(11, 9) → 11      0      X
max1020(9, 21) → 0      0      OK
max1020(10, 21) → 10      0      X
max1020(21, 10) → 10      0      X
max1020(9, 11) → 11      0      X
max1020(23, 10) → 10      0      X
max1020(20, 10) → 20      10      X
max1020(7, 20) → 20      0      X
max1020(17, 16) → 17      16      X
Author Commented:
``````public int max1020(int a, int b) {

if(a>=10 && a<=20 && b>=10 && b<=20)
{
if(a>b)
{
return a;
}
return b;
}
return 0;

}
``````

modified as above still failing below

Expected      Run
max1020(11, 19) → 19      19      OK
max1020(19, 11) → 19      19      OK
max1020(11, 9) → 11      0      X
max1020(9, 21) → 0      0      OK
max1020(10, 21) → 10      0      X
max1020(21, 10) → 10      0      X
max1020(9, 11) → 11      0      X
max1020(23, 10) → 10      0      X
max1020(20, 10) → 20      20      OK
max1020(7, 20) → 20      0      X
max1020(17, 16) → 17      17      OK
Your progress graph for this problem
Author Commented:
max1020(11, 9) → 11      0      X

how this is 11. I thought it supposed to return 0 as per challenge.

Commented:
You want to return 0 only when neither is in that range.
You are returning 0 unless both are in that range.

Also, you want the largest number, not the number that is closest to 10
Commented:
You corrected the part about returning the largest number instead of the number closest to 10
but are you understanding what " if neither is in that range. " means?
Author Commented:
" if neither is in that range. "

yes

like 5,14 for  a and b
Commented:
Is a in that range?
Is b in that range?
Are neither in that range?
Author Commented:
``````public int max1020(int a, int b) {

if((a>=10 && a<=20)&&(b>=10 && b<=20))
{
if(a>b)
{
return a;
}
return b;
}
else if(a>=10 && a<=20)
return a;

else if(b>=10 && b<=20)
return b;

else

return 0;

}
``````

this seems all passing
Commented:
I agree
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.