943,549 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 749
  • Java RSS
Jul 3rd, 2009
0

Java loop error?

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  1. for (j = y1; j != y2; y1 < y2 ? j++ : j--)
  2. {
  3. if(j % 4 == 0)
  4. {
  5. if (y1 % 100 != 0)
  6. {
  7. totalDays += 1;
  8. return "The first year entered is a leap year\n.";
  9. else if (y1 % 400 == 0)
  10. {
  11. totalDays += 1;
  12. return "The first year entered is a leap year\n.";
  13. }
  14. }
  15. else
  16. totalDays = totalDays;
  17. return "The first year entered is NOT a leap year\n.";
  18. }
  19. }
  20. int yearsDifference = y2-y1;
  21. totalDays += 365*yearsDiference;
  22. }
  23. }

The output I get:

Java Syntax (Toggle Plain Text)
  1. DateDifference.java:148: not a statement
  2. for (j = y1; j != y2; y1 < y2 ? j++ : j--)
  3. ^
  4. DateDifference.java:156: 'else' without 'if'
  5. else if (y1 % 400 == 0)
  6. ^
  7. 2 errors
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kssi89 is offline Offline
24 posts
since Apr 2009
Jul 3rd, 2009
0

Re: Java loop error?

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/...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:

Java Syntax (Toggle Plain Text)
  1. public class YourClass extends Date{
  2. @Override
  3. public int compareTo(Object o){
  4. //Your code for comparing the dates goes here.
  5. }
  6. }

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.
Last edited by BestJewSinceJC; Jul 3rd, 2009 at 5:51 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jul 4th, 2009
0

Re: Java loop error?

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/...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:

Java Syntax (Toggle Plain Text)
  1. public class YourClass extends Date{
  2. @Override
  3. public int compareTo(Object o){
  4. //Your code for comparing the dates goes here.
  5. }
  6. }

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kssi89 is offline Offline
24 posts
since Apr 2009
Jul 4th, 2009
0

Re: Java loop error?

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Jul 4th, 2009
1

Re: Java loop error?

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))
Featured Poster
Reputation Points: 1907
Solved Threads: 947
Posting Expert
JamesCherrill is offline Offline
5,754 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Can we make Java web report just by drag and drop?
Next Thread in Java Forum Timeline: Not getting the right movement of the circle





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC