Hello!

I'm taking a class in JAVA and learning a lot, but i'm stumped on a project where we are to find the difference between two dates. The method iv'e used is a little unconventional, I don't know if there is aproblem with the way i've structured the loop below...

The y1 < y2 ? j++: j-- is supposed to acount for the user entering a date with alower value before a date with a higher year value or the other way around....because the first year being less than the second year, and the first year being equal to j, will force j to increment by one. Else it will decrement by 1 until j is equal to year 2.

for (j = y1; j != y2; y1 < y2 ? j++ : j--)
			{
			if(j % 4 == 0)
				{
					if (y1 % 100 != 0)
					{
						totalDays += 1;
						return "The first year entered is a leap year\n.";
						else if (y1 % 400 == 0)
						{
							totalDays += 1;
							return "The first year entered is a leap year\n.";
						}
					}
					else
					totalDays = totalDays;
					return "The first year entered is NOT a leap year\n.";
				}
			}
			int yearsDifference = y2-y1;
			totalDays += 365*yearsDiference;
	}
}

The output I get:

DateDifference.java:148: not a statement
				for (j = y1; j != y2; y1 < y2 ? j++ : j--)
				                              ^
DateDifference.java:156: 'else' without 'if'
						else if (y1 % 400 == 0)
						^
2 errors

I don't understand your code, but it shouldn't matter what order the user enters the Dates in. You can still get the difference between them either way. Also:

Use classes that already exist. http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html

If you wanted to, you could extend Date and override the compareTo method, making it return the number of years difference between the two Dates. This way of doing things would be a lot easier than what you are doing right now. Essentially, to extend the Date class and override the compareTo method, you would declare your class like this:

public class YourClass extends Date{
@Override
public int compareTo(Object o){
//Your code for comparing the dates goes here.
}
}

Alternatively, you could write your own Date class that had the variables you want (such as Month, Day, Year), and write a method that compares two dates in there.

I don't understand your code, but it shouldn't matter what order the user enters the Dates in. You can still get the difference between them either way. Also:

Use classes that already exist. http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html

If you wanted to, you could extend Date and override the compareTo method, making it return the number of years difference between the two Dates. This way of doing things would be a lot easier than what you are doing right now. Essentially, to extend the Date class and override the compareTo method, you would declare your class like this:

public class YourClass extends Date{
@Override
public int compareTo(Object o){
//Your code for comparing the dates goes here.
}
}

Alternatively, you could write your own Date class that had the variables you want (such as Month, Day, Year), and write a method that compares two dates in there.

That's neat, but i'm suposed to construct the class and I have done exactly what you've said in the bottom, write a method to accept 2 dates, parse 3 values using substring and store them in 6 variables, each one corresponding to day1, month1, year1 & day2, month2, year2. I need to find the difference between day & month too aswell as the year and have used switch/case because it's what we know at this stage.

My problems come with the loop and if/else statement... i'm not sure what the program expects or if what i've done with the forked j++ or j-- alternatives based on user input is appropriate for JAVA.

Regarding the ternary operator inside the for-statement, I've never been any good with the ternary operator and rarely use it. I've never seen it inside of a for-statement though, which doesn't mean it can't be done, just that I've never seen it. So I can be of no help in correcting the for-statement to allow for the ternary operator. If it were me, I'd rewrite it so that any ternary statement wasn't in the for-statement, but that's because, again, I'm not very knowledgeable in its use.

The else-if error is easier. Here's your code:

if (y1 % 100 != 0)
{
	totalDays += 1;
	return "The first year entered is a leap year\n.";
	else if (y1 % 400 == 0)
	{
		totalDays += 1;
		return "The first year entered is a leap year\n.";
	}
}
else
     totalDays = totalDays;

The red if and else go together, but you have a green "else if" with no matching "if". You can't have that. You have an unconditional return statement directly above the "else if" anyway, so it's an unreachable statement anyway.

Your ternary expr is almost there, just needs (1) brackets to get the order of evaluation right and (2) it has to be used in an assignment kind of way

for (j = y1 ; j != y2; j = j + ((y1 < y2) ? 1 : -1))

Comments
I was playing around with that ternary operator earlier. I got close, but not quite. This makes sense now.
This article has been dead for over six months. Start a new discussion instead.