Reputation: 3
I am stuck on this question.
Write a program that would input two dates(month and day only) and display how many days there are between two dates. Assume the two dates are within the year 2013. The program must validate each date entered to check whether the day is between 1 and the maximum numbers of days of that month. Use an array to store the maximum number of days per month and output the number of days elapsed.
Removed the code due to experiences with peers/acquaintances copying my code.
Problem is whenever I input January 1
as the first date and April 1
as the second date, it only outputs 31 days while the real answer is 90 days. Can anyone help?
edit:
Its working properly for now. Thanks for those who helped.
Upvotes: 0
Views: 1082
Reputation: 45080
Your logic is flawed. You need to do something like this:-
int start = 0;
int middle = 0;
int end = 0;
int sum = 0;
...
if (code2 > code) {
start = Days[code] - inputDay + 1;
for (int count = inputMonth; count < code2; count++) {
middle += Days[count];
}
end = inputDay2;
} else {
start = inputDay2 - inputDay + 1;
}
sum = start + middle + end;
Upvotes: 0
Reputation: 1508
Your condition in the for loop used a > when it should be <
for (int count = inputMonth; count < inputMonth2; count++) {
middle = middle + Days[count];
}
Your code contains a multitude of logical errors (for example: what happens when 2 dates in the same month are used), below is a working version:
public static void main(String[] args) {
String monthStr;
String dayStr;
int inputMonth;
int inputDay;
String monthStr2;
String dayStr2;
int inputMonth2;
int inputDay2;
int code;
int code2;
int start;
int middle = 0;
int end;
int sum;
String[] Months = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December" };
int[] Days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
monthStr = JOptionPane.showInputDialog(null, "Enter First Month: ");
inputMonth = Integer.parseInt(monthStr);
dayStr = JOptionPane.showInputDialog(null, "Enter First Day: ");
inputDay = Integer.parseInt(dayStr);
monthStr2 = JOptionPane.showInputDialog(null, "Enter Second Month: ");
inputMonth2 = Integer.parseInt(monthStr2);
dayStr2 = JOptionPane.showInputDialog(null, "Enter Second Day: ");
inputDay2 = Integer.parseInt(dayStr2);
if (inputMonth == inputMonth2) {
// Same months
sum = inputDay2 - inputDay;
} else {
// Different months
start = Days[inputMonth - 1] - inputDay;
end = inputDay2;
middle = 0;
// Start at inputMonth+1, start already included the days remaining
// in inputMonth
for (int count = inputMonth + 1; count < inputMonth2; count++) {
middle = middle + Days[count - 1];
}
sum = start + middle + end;
}
JOptionPane.showMessageDialog(null, "DAYS COMPUTATION PROGRAM"
+ "\nFirst Date: " + Months[inputMonth - 1] + " " + inputDay
+ "\nSecond Date: " + Months[inputMonth2 - 1] + " " + inputDay2
+ "\nNumber of Days Elapsed: " + sum + " Days");
}
Upvotes: 0
Reputation:
Use java.util.Calendar
:
int diff = 0;
Calendar begin = Calendar.getInstance();
begin.set(Calendar.MONTH, beginMonth);
begin.set(Calendar.DAY_OF_MONTH, beginDay);
Calendar end = Calendar.getInstance();
end.set(Calendar.MONTH, endMonth);
end.set(Calendar.DAY_OF_MONTH, endDay);
while(begin.compareTo(end) <= 0) {
diff++;
begin.add(Calendar.DAY_OF_MONTH, 1);
}
System.out.println(diff);
This way the API itself takes care of the number of days in months. Also this is much more flexible as you can also find the diff even if the given dates don't fall in the same year (requires small change in code).
Upvotes: 0
Reputation: 18568
public int daysBetween(Date d1, Date d2){
return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
}
from Getting the number of days between two dates in java
Upvotes: 0
Reputation: 19856
change
for (int count = inputMonth; count > inputMonth2; count++)
to
for (int count = inputMonth; count < inputMonth2; count++)
Upvotes: 2