| | |
Java loop error?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 24
Reputation:
Solved Threads: 0
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.
The output I get:
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)
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:
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Sep 2008
Posts: 1,647
Reputation:
Solved Threads: 206
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:
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.
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)
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.
Last edited by BestJewSinceJC; Jul 3rd, 2009 at 5:51 pm.
Out.
•
•
Join Date: Apr 2009
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
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)
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
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:
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.
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.
![]() |
Similar Threads
- Exception in thread "main" java.lang.NoClassDefFoundError: Invaders Error (Java)
- Java Heap Space Error (Java)
- java runtime error for database connection (Java)
- Help with error in using iterator in a for loop (C++)
- New to java I need help with switch case error (Java)
- Error running Java (Java)
- Java Encryption error (Java)
- for loop error (C)
Other Threads in the Java Forum
- Previous Thread: Can we make Java web report just by drag and drop?
- Next Thread: Not getting the right movement of the circle
Views: 459 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api appinventor apple applet application arguments array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component database draw eclipse error event exception file fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaprojects jetbrains jmf jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie notdisplaying number object oracle page print problem program programming project qt recursion scanner screen server set size sms socket sort spamblocker sql string swing system test threads time transfer tree variablebinding windows xor






