Search code examples
javafor-loopprintlnleap-year

Java - Printing new line after ten values in a For loop


I have been trying to make a simple program that will print out the leap years between 2 inputted years. I have made the loop that checks each year to see if it is a leap year and then prints it, but I would like to be able to only show 10 years on each line, not just all the of them on one long line.

So like this:

    1004, 1008, 1012, 1016, 1020, 1024, 1028, 1032, 1036, 1040,
    1044, 1048, 1052, 1056, 1060, 1064, 1068, 1072, 1076, 1080,
    1084, 1088, 1092, 1096, 1104, 1108, 1112, 1116, 1120, 1124,
    1128, 1132, 1136, 1140, 1144, 1148, 1152, 1156, 1160, 1164,
    1168, 1172, 1176, 1180, 1184, 1188, 1192, 1196, 1200.

instead of this:

1004, 1008, 1012, 1016, 1020, 1024, 1028, 1032, 1036, 1040, 1044, 1048, 1052... 

I have tried various different methods to make this happen, nested for loops etc but at the moment I cant seem to make it happen. Sorry if it's a stupid question with a simple answer, I haven't been learning Java for very long at all.

Here is my code so far which prints all the years on one line:

import java.util.Scanner;

public class LeapYears {

    public static void main(String[] args) 
    {
        int firstY, finalY;
        Scanner kb = new Scanner(System.in);

        System.out.print("Enter start year: ");
        firstY = kb.nextInt();

        System.out.print("Enter end year: ");
        finalY = kb.nextInt();

        for(; firstY <= finalY; firstY++) {
            if ((firstY % 4) == 0) {
                if ((firstY % 100) == 0) {
                    if ((firstY % 400) == 0 ) {
                        System.out.print(firstY + ", ");
                        firstY++;
                    }
                } else {
                    System.out.print(firstY + ", ");
                }
            }
        }
    }
}

Solution

  • This is what I would do. Simply add an int counter variable, increment it every time that you print out a year. When it reaches a multiple of 10, simply print a new line.

    import java.util.Scanner;
    
    public class LeapYears 
    {
    
    public static void main(String[] args) 
    {
        int firstY, finalY, counter;
        counter = 0;
        Scanner kb = new Scanner(System.in);
    
        System.out.print("Enter start year: ");
        firstY = kb.nextInt();
    
        System.out.print("Enter end year: ");
        finalY = kb.nextInt();
    
    
    
        for(; firstY <= finalY; firstY++)
    
        {
            if ((firstY % 4) == 0)
    
            {
                if ((firstY % 100) == 0)
    
                {
                    if ((firstY % 400) == 0 )
    
                    {
                        System.out.print(firstY + ", ");
                        firstY++;
                        counter++;
                        if (counter % 10 == 0) System.out.println("");
                    }
                }
    
                else
                {
                    System.out.print(firstY + ", ");
                }
    
            }
        }
    
    }
    }