I have a function that takes in a zip code and assigns it a number based on the range it falls into. I want it to fail if it doesn’t fall into one of the ranges. what do I do?
/**
* Calculates zone number from second zip code
* @param endZip
* @return endZone
*/
public static int endZone(int endZip)
{
int endZone = 0;
//zone 1
if (endZip >= 00001 && endZip <= 6999)
{
endZone = 1;
}
//zone 2
else if (endZip >= 07000 && endZip <= 19999)
{
endZone = 2;
}
//zone 3
else if (endZip >= 20000 && endZip <= 35999)
{
endZone = 3;
}
//zone 4
else if (endZip >= 36000 && endZip <= 62999)
{
endZone = 4;
}
//zone 5
else if (endZip >= 63000 && endZip <= 84999)
{
endZone = 5;
}
//zone 6
else if (endZip >= 85000 && endZip <= 99999)
{
endZone = 6;
}
return endZone;
}
If for some reason you ABSOLUTELY want to return null, the simple thing is to switch the return type from an int to an Integer object, which unlike primitives can be null.
simply change the signature to:
and the initialization to:
but really the other answers are better.