![]() |
| ||
| Java loop error? 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--) The output I get: DateDifference.java:148: not a statement |
| ||
| 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: public class YourClass extends Date{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. |
| ||
| Re: Java loop error? Quote:
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. |
| ||
| 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) 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. |
| ||
| 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)) |
| All times are GMT -4. The time now is 5:30 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC