clink
clink

Reputation: 213

Using Java string.format() for printing

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.

Upvotes: 1

Views: 3807

Answers (3)

RBH
RBH

Reputation: 271

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]

Upvotes: 1

Paras
Paras

Reputation: 338

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));   
            }
        }
    }
}

Upvotes: 0

xingbin
xingbin

Reputation: 28279

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(",");
        }
    }
}

Upvotes: 1

Related Questions