3
\$\begingroup\$

I've made a working calendar program, BUT I believe it's extremely inefficient. I think I have way too many else if statements:

import java.util.*;
public class MyOwnCalendar {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);


        System.out.print("Enter the number of days in month: ");
        int days = keyboard.nextInt();

        System.out.print("Enter which day the first day is on: ");
        int firstDay = keyboard.nextInt();

        System.out.println("S   M   T   W   Th  F   Sa");

        int ctr = 1;

        if (days == 31 && firstDay == 1)
        {
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 8 || ctr == 15 || ctr == 22 || ctr == 29)
                {
                    System.out.println();
                }
            }
        }
        else if (days == 31 && firstDay == 2)
        {
            System.out.print("\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 7 || ctr == 14 || ctr == 21 || ctr == 28)
                {
                    System.out.println();
                }
            }
        }
        else if (days == 31 && firstDay == 3)
        {
            System.out.print("\t\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 6 || ctr == 13 || ctr == 20 || ctr == 27)
                {
                    System.out.println();
                }
            }
        }
        else if (days == 31 && firstDay == 4)
        {
            System.out.print("\t\t\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 5 || ctr == 12 || ctr == 19 || ctr == 26)
                {
                    System.out.println();
                }
            }
        }
        else if (days == 31 && firstDay == 5)
        {
            System.out.print("\t\t\t\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 4 || ctr == 11 || ctr == 18 || ctr == 25)
                {
                    System.out.println();
                }
            }
        }
        else if (days == 31 && firstDay == 6)
        {
            System.out.print("\t\t\t\t\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 3 || ctr == 10 || ctr == 17 || ctr == 24 || ctr == 31)
                {
                    System.out.println();
                }
            }
        }
        else if (days == 31 && firstDay == 7)
        {
            System.out.print("\t\t\t\t\t\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 2 || ctr == 9 || ctr == 16 || ctr == 23 || ctr == 30)
                {
                    System.out.println();
                }
            }
        }
        else if (days == 30 && firstDay == 1)
        {
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 8 || ctr == 15 || ctr == 22 || ctr == 29)
                {
                    System.out.println();
                }
            }

        }
        else if (days == 30 && firstDay == 2)
        {
            System.out.print("\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 7 || ctr == 14 || ctr == 21 || ctr == 28)
                {
                    System.out.println();
                }
            }

        }
        else if (days == 30 && firstDay == 3)
        {
            System.out.print("\t\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 6 || ctr == 13 || ctr == 20 || ctr == 27)
                {
                    System.out.println();
                }
            }
        }
        else if (days == 30 && firstDay == 4)
        {
            System.out.print("\t\t\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 5 || ctr == 12 || ctr == 19 || ctr == 26)
                {
                    System.out.println();
                }
            }
        }
        else if (days == 30 && firstDay == 5)
        {
            System.out.print("\t\t\t\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 4 || ctr == 11 || ctr == 18 || ctr == 25)
                {
                    System.out.println();
                }
            }
        }
        else if (days == 30 && firstDay == 6)
        {
            System.out.print("\t\t\t\t\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 3 || ctr == 10 || ctr == 17 || ctr == 24 || ctr == 31)
                {
                    System.out.println();
                }
            }
        }
        else if (days == 30 && firstDay == 7)
        {
            System.out.print("\t\t\t\t\t\t");
            while (ctr <= days)
            {
                System.out.print(ctr);
                System.out.print("  ");
                ctr++;
                if (ctr == 2 || ctr == 9 || ctr == 16 || ctr == 23 || ctr == 30)
                {
                    System.out.println();
                }
            }
        }

    }

}

What are some easier ways to do this? Would a for loop have made this easier?

\$\endgroup\$
4
\$\begingroup\$

This is a classic example of where the Modulo operator is useful.

The modulo allows you to determine where in a sequence you hit a repeating part of the pattern. For example, you are processing thousands of lines of data, so you want to log some output, you print a message every 1000 lines with:

if (count % 1000 == 0) {
    System.out.println("Processing line " + count);
}

We can use the modulo to our advantage with weeks having 7 days....

So, if we do something like:

for (int i = 1; i <= daysinmonth; i++) {
    System.out.print(" " + i);
}

we will just get numbers printed accross the screen... but, if we do:

for (int i = 1; i <= daysinmonth; i++) {
    System.out.print(" " + i);
    if (i % 7 == 0) {
        System.out.println();
    }
}

then the numbers will wrap neatly.

Now, if you take that example, and extend it to start at a certain day of the week, you can do:

// note the start-at-1 offset.
for (int i = 1; i < firstDay; i++) {
    System.out.print("   "); // some spaces for the starting days.
}

now we do the same loop as before, but we offset our line-break:

for (int i = 1; i <= days; i++) {
    System.out.print(" " + i);
    if ((i + firstDay) % 7 == 0) {
        System.out.println();
    }
}

Using this logic you should be able to bring your code down to just two simple loops, a padding loop, and a number loop.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.