Results 1 to 3 of 3
  1. #1
    captj is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default equalsIgnoreCase() and Throwing an expection question

    I'm working on a constructor can accepts a String and will change the string into a int to set to the monthNumber field. If the String isn't valid the constructor should throw the exception listed. This seems to work when I input a string of 1-12, however whenever I try to input a month name via interactionsI'm getting a
    "Exception in evaluation thread java.lang.NumberFormatException: For input string: "December""
    exception.
    Any advice?
    Java Code:
       public class Month
       {
       
       // Variables
          private int monthNumber;
          private static String[] monthNames = {"January", "February", "March", "April",
             "May", "June", "July", "August", "September", "October", "November", "December"};   
             
       public Month(String s)
          {
          // Check if s is a number 1-12
             if (Integer.parseInt(s) >= 1 && Integer.parseInt(s) <= 12)
                monthNumber = Integer.parseInt(s);
             else
             {
             // Check if s is a month
                for(int count = 0; count < monthNames.length; count++)
                {
                // Compare monthNames with s while ignoring case to find a match
                   if (monthNames[count].equalsIgnoreCase(s))
                      monthNumber = count + 1;
                }
             }
             // If we still have not found a match, thow an expection
             if (monthNumber == 0)
                throw new IllegalArgumentException("Illegal month number: " + s);
          }
    Last edited by captj; 04-05-2012 at 11:12 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: equalsIgnoreCase() and Throwing an expection question

    however whenever I try to input a month name via interactionsI'm getting a
    See the API for Integer - December is not a number, so Integer.parseInt on this string (as well as all other strings that are not numbers) will throw a NumberFormatException. You must change the logic in your code to take this into account.

  3. #3
    captj is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: equalsIgnoreCase() and Throwing an expection question

    Quote Originally Posted by doWhile View Post
    See the API for Integer - December is not a number, so Integer.parseInt on this string (as well as all other strings that are not numbers) will throw a NumberFormatException. You must change the logic in your code to take this into account.
    Thank you! I got it down now. For anyone wondering here is the corrected code.

    Java Code:
       // String constructor
          public Month(String s)
          {
          // Check if s is a number 1-12
             try
             {
                if (Integer.parseInt(s) >= 1 && Integer.parseInt(s) <= 12)
                   monthNumber = Integer.parseInt(s);
             }
                catch (IllegalArgumentException e)
                {
                // Check if s is a month
                   for(int count = 0; count < monthNames.length; count++)
                   {
                   // Compare monthNames with s while ignoring case to find a match
                      if (monthNames[count].equalsIgnoreCase(s))
                         monthNumber = count + 1;
                   }
                }
    All I had to do was change it into a try catch argument. Pretty simple in the end.

Similar Threads

  1. Replies: 4
    Last Post: 02-23-2012, 08:43 PM
  2. Exception throwing
    By andy16 in forum New To Java
    Replies: 9
    Last Post: 06-07-2010, 05:57 PM
  3. the opposite of equalsignorecase?
    By Meta in forum New To Java
    Replies: 2
    Last Post: 03-17-2010, 04:37 AM
  4. comparing 3 strings with equalsignorecase
    By vividcooper in forum New To Java
    Replies: 4
    Last Post: 01-13-2010, 09:30 PM
  5. throwing bug value
    By Dangi in forum New To Java
    Replies: 3
    Last Post: 10-25-2008, 06:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •