Java loop error?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 24
Reputation: kssi89 is an unknown quantity at this point 
Solved Threads: 0
kssi89 kssi89 is offline Offline
Newbie Poster

Java loop error?

 
0
  #1
Jul 3rd, 2009
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.

  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:

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Java loop error?

 
0
  #2
Jul 3rd, 2009
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:

  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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 24
Reputation: kssi89 is an unknown quantity at this point 
Solved Threads: 0
kssi89 kssi89 is offline Offline
Newbie Poster

Re: Java loop error?

 
0
  #3
Jul 4th, 2009
Originally Posted by BestJewSinceJC View Post
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Java loop error?

 
0
  #4
Jul 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 976
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Java loop error?

 
1
  #5
Jul 4th, 2009
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))
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC