I'm trying to account for any value outside of 1 to 12 or a name of a month. I'm passing in a string to this method. I then try to parse it into an int and do an if statement to see if it's between 1 and 12. If it's not an int then it goes down and a for-loop checks to see if it's a name of a month.
The problem is that if I add a throw new IllegalArgument Exception to the catch block and pass in "Saturday" it throws the Illegal month number here message instead of Invalid month name string message. If I leave the catch block empty, then numbers outside the range of 1 to 12 go down and throw the Invalid month name string message.
The problem is that if I add a throw new IllegalArgument Exception to the catch block and pass in "Saturday" it throws the Illegal month number here message instead of Invalid month name string message. If I leave the catch block empty, then numbers outside the range of 1 to 12 go down and throw the Invalid month name string message.
Code:
public void setMonthName(String inSetString)
{
int i = 0;
monthNumber = -1;
try
{
i = Integer.parseInt(inSetString);
if (i >= 1 && i <= 12)
{
monthNumber = i;
}
}
catch (IllegalArgumentException e)
{
}
// if it gets to here we know it's a string
for (int index = 0; index < monthNames.length; index++)
{
if (monthNames[index].equalsIgnoreCase(inSetString))
{
monthNumber = index;
}
}
if (monthNumber == -1)
{
throw new IllegalArgumentException("Invalid month name string: " + inSetString);
}
}
Comment