1

I'm new to java and i have written a code to print out the leap years between a certain range and for the output, i want to print out 3 leap years per line, with each year separated by commas.

For example, between the year 1999 to 2045, the output i want is:

2000,2004,2008
2012,2016,2020
2024,2028,2032
2036,2040,2044

I have written the code:

for (int year = 1999; year<2045; year ++) {
            if (((year%4 == 0) && (year%100 !=0)) || (year%400==0)) {
                System.out.println(String.format(format, year));   #issue here
            }
}

I'm confused on how the String format works through the String.format notation that i plan to use when printing. Would appreciate some help on this.

3
  • 4
    You only have one year available to print at a time with the current design, so print formatting 3 comma-separated years won't work until you add a lot of ugly code around, such as a counter to reach 3 / end of iteration. Also on a side-note it's pointless to wrap String.format within System.out.print... when you can just System.out.printf. Commented Apr 17, 2018 at 12:54
  • This might help you tou understand how format works - docs.oracle.com/javase/tutorial/java/data/numberformat.html Commented Apr 17, 2018 at 12:58
  • 1
    I don't think String.format() is a good candidate for doing this. Either you are using it at a wrong place or you didn't understand your homework question :P Commented Apr 17, 2018 at 13:21

3 Answers 3

1

String.format() roughly takes a first string argument that is the "layout" you desire, with specifiers that start with % representing the variable types (%s for string, %d for digit etc.) and the next series of arguments being the actual data - that should match the number of specifiers in the format layout in number and order:

    int[] years = new int[3];
    int i = 0;
    for (int year = 1999; year < 2045; year++) {
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
            years[i++] = year;
            if (i > 2) {
                System.out.println(String.format("%d,%d,%d", years[0], years[1], years[2]));
                i = 0;
            }
        }
    }

2000,2004,2008
2012,2016,2020
2024,2028,2032
2036,2040,2044

String.format() feels like overkill for this situation though. You could (approximately) accomplish the same task in my particular solution without the need for format by just using:

 System.out.println(Arrays.toString(years));

except that there would also be square brackets around the int array used in this example.

[2000, 2004, 2008]
[2012, 2016, 2020]
[2024, 2028, 2032]
[2036, 2040, 2044]
Sign up to request clarification or add additional context in comments.

Comments

1

This can not be achieved without some condition statements, better to do it this way:

for (int year = 1999; year < 2045; year++) {
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {         
        System.out.print(year);
        column++;
        if (column % 3 == 0) {
            System.out.println();
        } else {
            System.out.print(",");
        }
    }
}

6 Comments

This will work fine only in single thread mode. Otherwise other threads may interfere in your printing. So, don't use that.
@Imaskar considering the OP is new to Java, he probably isn't printing from other threads. While your comment is technically true, it's not a very realistic possibility.
@Kayaman is it an excuse to stick with bad practices? He will remember it and make mistakes later. Just swap System.out with StringBuilder and print it at the end and it will do.
@Imaskar stop exaggerating. There's a lot of worse things he'll be doing before he even gets to writing multi-threaded code.
@Imaskar Sadly, StringBuilder is not thread safe too...stackoverflow.com/questions/48558432/…
|
0

you can use %tY as format for year.it will format year in four digits.

String.format("%tY", year)

here is the code i have tried:

import java.util.Calendar;

public class MyClass {
    public static void main(String args[]) {
        int year;
          Calendar cal = Calendar.getInstance();
        for (year = 1999; year<2045; year ++) {
            if (((year%4 == 0) && (year%100 !=0)) || (year%400==0)) {
                cal.set(year, 0, 0);
                System.out.println(String.format("%1$tY", cal));   
            }
        }
    }
}

1 Comment

This doesn't look like what op had asked. He wanted 3 in a row.

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.