Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7870811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:43:31+00:00 2026-06-03T01:43:31+00:00

I am new to Programming, and to Java, and I’m trying to teach myself

  • 0

I am new to Programming, and to Java, and I’m trying to teach myself by working through the Project Euler website. I am trying to complete this problem: http://projecteuler.net/problem=19, which is:

How many Sundays fell on the first of the month during the twentieth
century (1 Jan 1901 to 31 Dec 2000)?

The way I thought to solve it, was to make a 2D array that represents a calander, and to loop through the array by counting to 7, and then each time I count to 7, add 1 to that point in the array. At the end, I will sum the first row of the array, and that should be how many sundays were on the first of the month.

But I am having trouble with my loops, my counting to 7 resets when it gets to the end of a month, and I can’t figure out how to stop it from doing that?

Here is my code:

public class Problem019 {
    public static void main (String[] args){

        //System.out.println(LeapYearTest(1996));
        int ThirtyOne = 31;
        int Thirty = 30;
        int FebNorm = 28;
        int FebLeap = 29;
        int a, b, c, Day, e = 0, f = 0;
        int Calander[] []= new int [12] [] ;

        Calander[0] = new int [ThirtyOne];
        Calander[1] = new int [FebNorm];
        Calander[2] = new int [ThirtyOne];
        Calander[3] = new int [Thirty];
        Calander[4] = new int [ThirtyOne];
        Calander[5] = new int [Thirty];
        Calander[6] = new int [ThirtyOne];
        Calander[7] = new int [ThirtyOne];
        Calander[8] = new int [Thirty];
        Calander[9] = new int [ThirtyOne];
        Calander[10] = new int [Thirty];
        Calander[11] = new int [ThirtyOne];

        for (a=1901;a<2001;a++){
            //System.out.println(a);
            if (LeapYearTest(a))
            {
                Calander[1] = new int [FebLeap];
            }
            else
            {
                Calander[1] = new int [FebNorm];
            }

            for (e=0;e<Calander.length;e++)
            {   
                System.out.println("e: " + e);
                f=0;

                while (f<Calander[e].length)
                {   

                    //System.out.println(Calander[e].length);
                    Day=1;
                    while (Day<8 && f<Calander[e].length)
                    {   
                        System.out.println("f: " + f + "\tDay: " + Day + "\tCalander[e][f]: " + Calander[e][f]);
                        Day++;
                        f++;

                        if (f<Calander[e].length && f!=0 && Day==7)
                        {
                        Calander[e][f]+= 1;
                        }

                    }

                }
            }
            //System.out.println(a);
        }
        for (b=0;b<Calander.length;b++)
        {   
            System.out.print(Calander[0][b]);
        }
    }   


    public static boolean LeapYearTest(int x)
    {
        if (x%4==0 || x%400==0){
            return true;
        }
        if (x%100==0){
            return false;
        }
        else return false;
    }

}

This is what it prints, e is the month, f is the days in the month, and Day is counting to 7:

f: 25   Day: 5  Calander[e][f]: 0
f: 26   Day: 6  Calander[e][f]: 0
f: 27   Day: 7  Calander[e][f]: 100
f: 28   Day: 1  Calander[e][f]: 0
f: 29   Day: 2  Calander[e][f]: 0
**f: 30 Day: 3  Calander[e][f]: 0**
e: 10
**f: 0  Day: 1  Calander[e][f]: 0**
f: 1    Day: 2  Calander[e][f]: 0
f: 2    Day: 3  Calander[e][f]: 0

How can I set up the loops so that Day doesn’t reset at the end of the month? Or is there another way to solve this problem that doesn’t involve so many nested loops?

Thankyou!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:43:32+00:00Added an answer on June 3, 2026 at 1:43 am

    I think you need to toss out your existing code and start fresh. Since you’re trying to learn how to code by solving Project Euler problems, I won’t ruin the fun for you by giving you the code. It seems you do want the full working code, so I’ve fixed your code, including a few bugs that were present due to some subtle details in the problem statement that you may have misunderstood or overlooked.

    Just for fun, let’s take a look at the immediate problem with your code that you want fixed…

    When you initially declare Day, initialize it to 1. Then replace this line:

    Day=1;
    

    with this:

    if (Day > 7) {
        Day = 1;
    }
    

    and move it inside of the loop that goes over the days of the month.

    But there’s still a serious problem. You keep overwriting your Feb array every year. You should only initialize it once, and set its length to 29. But this also has the unfortunate side effect of breaking any loops that depend on calendar[month].length, so you’ll have to account for that, too.

    All you really need to track are the number of Sundays that fell on the first of the month, so you just need to store and increment one variable. This solves the aforementioned problem with overwriting the Feb array, because you won’t use it (or any other month’s array) any more. On the other hand, if you really just want to practice using arrays, you could use a 3-dimensional array (in which the additional dimension is the year). But I’d venture to guess that most Java programmers use Lists instead of arrays most of the time, and when they do use arrays, they hardly ever use arrays with more than one dimension.

    A few more notes

    Your outer while loop is redundant.

    Your LeapYearTest method will incorrectly return true for all leap years divisible by 100 (all years divisible by 100 are also divisible by 4, so you’ll never enter the if block that tests years divisible by 100).

    At the end, you’re looping over every day of January (instead of looping over the first day of every month).

    Also note that the problem states,

    1 Jan 1900 was a Monday.

    But you’re supposed to find Sundays starting from 1 Jan 1901.

    After fixing these and other bugs (such as the conditions in your loops), I’ve included a fully working version of your code below. Note that you could easily optimize this to run in a fraction of the time by making more use of the modulus (%) operator and by not computing the number of Sundays on other days of the month (since you throw them away anyway in the end).

    public class Problem019 {
        public static void main (String[] args){
    
            final int ThirtyOne = 31;
            final int Thirty = 30;
            final int FebNorm = 28;
            final int FebLeap = 29;
            int numOfSundays = 0;
    
            int calendar[][]= new int [12][];
    
            calendar[0] = new int [ThirtyOne];
            calendar[1] = new int [FebLeap];
            calendar[2] = new int [ThirtyOne];
            calendar[3] = new int [Thirty];
            calendar[4] = new int [ThirtyOne];
            calendar[5] = new int [Thirty];
            calendar[6] = new int [ThirtyOne];
            calendar[7] = new int [ThirtyOne];
            calendar[8] = new int [Thirty];
            calendar[9] = new int [ThirtyOne];
            calendar[10] = new int [Thirty];
            calendar[11] = new int [ThirtyOne];
    
            int dayOfWeek = 1;
            for (int year = 1900; year < 2001; year++) {
                for (int month = 0; month < calendar.length; month++) {   
                    int dayOfMonth=0;
    
                    int daysInMonth;
                    if (month == 1) {
                        daysInMonth = isLeapYear(year) ? FebLeap : FebNorm;
                    }
                    else {
                        daysInMonth = calendar[month].length;
                    }
    
                    while (dayOfWeek < 8 && dayOfMonth < daysInMonth) {   
                        System.out.println("year: " + year + "\tday: " + dayOfWeek
                                + "\tcalendar["+month+"]["+dayOfMonth+"]: " + calendar[month][dayOfMonth]);
    
                        if (dayOfWeek == 7 && year > 1900) {
                            calendar[month][dayOfMonth]++;
    
                            if (dayOfMonth == 0) {
                                numOfSundays++;
                            }
                        }
    
                        dayOfMonth++;
    
                        dayOfWeek++;
                        if (dayOfWeek > 7) {
                            dayOfWeek=1;
                        }
                    }
                }
            }
    
            for (int month = 0; month < calendar.length; month++) {   
                System.out.println(calendar[month][0]);
            }
    
            System.out.println(numOfSundays);
        }   
    
        public static boolean isLeapYear(int year){
            if (year % 400 == 0) {
                return true;
            }
            else if (year % 100 == 0) {
                return false;
            }
            else if (year % 4 == 0){
                return true;
            }
            else {
                return false;
            }
        }
    }
    

    Again, this could be improved upon quite a lot. For example, you could simply loop over the years and months, and use Java’s built-in Calendar API or a third-party API, to check whether the first day of the month is a Sunday, but perhaps the coolest way to solve the problem is to implement the Doomsday Algorithm yourself. This will allow you to easily compute the day of the week for any given date, without using java.util.Calendar.

    Once you have implemented the Doomsday Algorithm, you don’t necessarily have to loop over all the months every time, so you could do even more optimizations. For instance, isSunday(MAR,1,year) == (! isLeapYear(year)) && isSunday(FEB,1,year).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Java programming, and this is actually part of a problem
I'm not new to Java programming in any way, but this problem is unheard
I'm still new to programming/Java/Android so I'm trying to understand everything I do and
I have been working on Eclipse recently. I am fairly new to java programming,
Before I start know this: I am extremely new to Java and programming. How
I'm quite new to Java Programming and am writing my first desktop app, this
I am new to thread programming in Java and hence this basic question. (I
I'm very new to Java programming language so this is probably dumb question but
I'm new to web java programming. I need to use this package org.apache.commons.collections .
new to Java programming, I am just trying to understand how I can make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.