Friday 4 May 2012

Program to find Mother's day date



Question: Use java language to write a program that could generate Mother’s Day date of each year. (Mother’s day located on 2nd week of May each year .)


Solution & problem faced: 

 
It’s seems perfect huh?
So let’s run it and see whether it could work correctly.
 
 
Oh my god. It’s failed. My code throw an exception named NumberFormatException. This exception is throw when execute the  line below:
Int year = Integer.parseInt(new String(b).trim());

At first, I thought it was because of I handle the data type incorrectly. I had a long try and research about it, but still the problem exist. Why why why? Long time struggle on it and finally I found out this is due I got the wrong format for the text file encoding.

 Path: Right click your .java file in eclipse>Properties>Resources>Text file encoding 





Lesson 1: Do pay attention to Text file encoding properties.  Make sure you know what encoding you would like to use.


Learning Source:



Now I change it back to US-ASC-II .  Run my code again and see whether it could work correctly. 
 


It seems correct right? I got the output. But when you see into details, you will find out that the date show is incorrect. Mother’s Day located on April? How come? It should located on May. Here’s come the second problem.


I got no idea how the problem could exist. But I guess it is because of my default setting for time zone and locale. By try an error, I add the code below to my program:
calendar.setFirstDayOfWeek (Calendar.MONDAY);       



Haha…It’s work finally. This is the output I get:
 



Final Code: 
 




1 comment: