0

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; 
}
Tim M.
  • 53,671
  • 14
  • 120
  • 163
user1378762
  • 53
  • 1
  • 9
  • Is 0 a valid "endZone"? Your code currently returns that if the zip doesn't fall inside your ranges. Can your caller just check that? In the end, it probably matters more what they calling code looks like than this – Gort the Robot May 07 '12 at 00:43
  • In that case, the caller can just do "int ez=endZone(zip);if(ez > 0) { //none error case} else { //error case }" wiht the method as written. – Gort the Robot May 07 '12 at 01:11
  • throw new Error("Invalid zip code."); – user1378762 May 07 '12 at 01:13
  • You would probably want to use a built-in exception class (e.g. IllegalArgumentException). Then of course you'd need to use try/catch blocks in the calling code. Throwing an exception like this is probably not something you want to do if you don't really understand the topic. Personally I would go the enum route... it makes for much more readable code, e.g.: if (zone == EndZone.Zone1) vs. if (zone == 1). That way the compiler will force you to always use a valid zone in your code. With intsyou could easily put if (zone == -19383) and the compiler would have no clue that there is a problem. – kad81 May 07 '12 at 01:27

2 Answers2

1

You have a few options:

If none of the conditions are satisfied, 1. Throw an exception. 2. Return a specific integer value that you know means that the zip was invalid (e.g. -1). 3. Instead of using an integer return type, create an enum for your zones:

public enum EndZone
{
    Zone1,
    Zone2,
    ....
    Invalid
}

Then return EndZone.Invalid if the conditions were not met.

kad81
  • 10,712
  • 3
  • 38
  • 44
  • how would I throw an exception? (I've just started coding recently) – user1378762 May 07 '12 at 01:05
  • Based on your code I assume you're working in Java. There are many websites devoted to explaining how exceptions work (try googling java throwing exceptions). – kad81 May 07 '12 at 01:11
  • A good discussion about whether an exception should be thrown or not for this type of situation can be found here: http://stackoverflow.com/questions/77127/when-to-throw-an-exception – kad81 May 07 '12 at 01:11
1

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:

public static Integer endZone(int endZip)

and the initialization to:

Integer endZone = null;

but really the other answers are better.

user439407
  • 1,666
  • 2
  • 19
  • 40