1

Firstly, bear with me – I'm only about a month into Java.

In an exercise, I'm asked to proof (with a test unit) that from a certain year (x) to a certain other year (y) that there are only one day between 31st of December and the 1st of January. They suggest that I should use a for-loop to make it run through all the years in-between our x and y year.

A predefined method called daysTill is already created.

So far, I've come up with this ugly piece of code, which doesn't work:

public void testYearEnd()
{int i;
       for(i = 1635; i <=2300; i++);
            Date date1 = new Date(i, 31, 12);
            Date date2 = new Date(i, 01, 01);
            assertEquals(1, date1.daysTill(date2));
}

Can anyone bear to point out exactly where my code is failing on me?

5
  • 2
    Remove ; after for; add braces around the loop body (indentation is not significant in Java). Commented Oct 4, 2014 at 1:25
  • Cheers, Jeff. Need to get into that mind set. Commented Oct 4, 2014 at 1:29
  • 1
    Two question style nits: it's "bear", not "bare" in "bare with me"/"can someone bear"; "thanks" and signatures shouldn't be in questions (you can thank by accepting and/or upvoting helpful answers, and your name is already on the question). You can edit the question if you like. (And welcome to Stack Overflow!) Commented Oct 4, 2014 at 1:35
  • 1
    While I understand this is an exercise, FYI, for real work we use a good date-time library. The java.util.Date and .Calendar classes bundled with Java are notoriously confusing and troublesome. Instead a popular replacement is eitherJoda-Time or the java.time package bundled with Java 8 (inspired by Joda-Time). Commented Oct 4, 2014 at 6:24
  • Interesting! Thanks for the insight – will keep in mind when I'm further into my study (BSc in Software). Commented Oct 4, 2014 at 8:45

1 Answer 1

2

Two problems here: you have a stray ; that's ending your for-statement without a body, making it a no-op, and missing braces around the intended body. (Without the ;, this wouldn't compile as the Date declaration isn't a statement.)

You can also move the declaration of i into the for-statement (you couldn't before because the for-statement ended early due to the ;, so i was undefined for the Date constructors).

The code should be

public void testYearEnd() {
    for (int i = 1635; i <= 2300; i++) {
        Date date1 = new Date(i, 31, 12);
        Date date2 = new Date(i, 01, 01);
        assertEquals(1, date1.daysTill(date2));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This definitely got some beginner mistakes out of the way. The code is nice and compilable now - but the test unit still wont accept it. It gives me an error 12 leading back to the source code of the "Date" class which works just fine, so there is still a logic issue in my code I think I'll need to work out myself. Thanks, anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.