HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaMinor

How to make this calender program simpler/optimized?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thismakeprogramoptimizedsimplercalenderhow

Problem

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

Solution

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.

Code Snippets

if (count % 1000 == 0) {
    System.out.println("Processing line " + count);
}
for (int i = 1; i <= daysinmonth; i++) {
    System.out.print(" " + i);
}
for (int i = 1; i <= daysinmonth; i++) {
    System.out.print(" " + i);
    if (i % 7 == 0) {
        System.out.println();
    }
}
// note the start-at-1 offset.
for (int i = 1; i < firstDay; i++) {
    System.out.print("   "); // some spaces for the starting days.
}
for (int i = 1; i <= days; i++) {
    System.out.print(" " + i);
    if ((i + firstDay) % 7 == 0) {
        System.out.println();
    }
}

Context

StackExchange Code Review Q#37278, answer score: 4

Revisions (0)

No revisions yet.