This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Oct 26, 2007 2:07 AM by masijade Go to original post RSS
  • 15. Re: Replacing String month with type Int - What to do!?!
    800282 Newbie
    Currently Being Moderated
    Jan.Gressmann wrote:
    ...
    default:
    System.out.println("Fatal Error");
    System.exit(0);
    return "Error"; //to keep the compiler happy
    }
    }
    No, don't do that. Let an exception be thrown or something, but don't use System.exit(...)!
    As already suggested: by using an array, the there would be less duplicayion of code:
        final static String[] MONTHS = {"January","February","March","April","May","June",
                        "July","August","September","October","November","December"};
    
        public String getMonthName(int number) {
            if(number < 1 || number > 12)
                throw new IllegalArgumentException("Number must be between 1 and 12!");
            return MONTHS[number-1];
        }
        
        public int getMonthNumber(String month) {
            for(int i = 0; i < MONTHS.length; i++) {
                if(MONTHS.equalsIgnoreCase(month)) return i+1;
    }
    return -1;
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
1 2 Previous Next