4

I have to output a Julian Calendar in java. I printed out each month, however I am unsure how to fix the row lengths to correspond to each month. For example, February, April, June, September and November do not have 31 days. This is my code so far:

 import java.text.DateFormatSymbols;

 public class testing {

 public void Day() {
   System.out.println();
   for (int i = 1; i <= 31; i++) {
     System.out.println(String.format("%2s", i));
   }
 }
 public void MonthNames() {
   String[] months = new DateFormatSymbols().getShortMonths();
   for (int i =  0; i < months.length; i++) {
     String month = months[i];
     System.out.printf(month + "   ");
     //System.out.printf("\t");       
    }    
  }

  public void ColRow() {
    int dayNum365 = 1;
    int array [][] = new int[31][12];
    System.out.println();
    for (int col = 0; col < 12; col++) {
      for (int row = 0; row < 31; row++) {
        array[row][col] += (dayNum365);
        dayNum365++;
      }
    }

    for (int col = 0; col < array.length; col++) {
     for (int row = 0; row < array[col].length; row++) {
        System.out.printf("%03d", array[col][row]);
        System.out.print("   ");
     }
    System.out.println();
    }
  }

  public static void main(String[] args) {
    testing calendar = new testing();
    calendar.MonthNames();
    calendar.ColRow();
  }
}
thegauravmahawar
  • 2,802
  • 3
  • 14
  • 23
ellier7
  • 417
  • 4
  • 9
  • 23
  • 2
    Dont create arrays at all. Use the already implemeted [GregorianCalendar](http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html). Or [JodaTime](http://www.joda.org/joda-time/), which is an external library but easier to handle. – Stefan Oct 03 '15 at 19:45

1 Answers1

12

We can create a matrix with a different number of columns for each row (called a jagged matrix) like this:

int[][] months = new int[12][];

months[0] = new int[31]; // January
months[1] = new int[28]; // February, assuming a non-leap year
months[2] = new int[31]; // March
// and so on...

Now, whenever we need to iterate over it, remember to take into account that each row will have a different length:

int dayNum365 = 1;
for (int month = 0; month < months.length; month++) {
    for (int day = 0; day < months[month].length; day++) {
        months[month][day] = dayNum365;
        dayNum365++;
    }
}

All of the above works because a 2D-matrix is nothing but an array of arrays, bear that in mind when working with matrices in Java.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • This solution works, however the rows and columns are switched. The calendar is now printing the 31 days across and 12 months down – ellier7 Oct 03 '15 at 20:02
  • @EJ then adjust your code accordingly. We can have a variable number of _columns_, but not a variable number of _rows_ in a matrix … which means that the part of your code that is supposed to print the days, is the one that is switched. – Óscar López Oct 03 '15 at 20:04
  • This is my print loop: for (int col = 0; col < 31; col++){ for (int row = 0; row < 12; row++){ System.out.printf("%03d", months[row][col]); System.out.print(" "); } System.out.println(); } – ellier7 Oct 03 '15 at 20:30
  • I am now getting an error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 28 at testing.display(testing.java:50) at testing.main(testing.java:61) – ellier7 Oct 03 '15 at 20:31
  • @EJ it means you tried to access a non existing index of the array like 30 in a month that has only 30 days. check your `for` loop condition. In adition to the answer you can also create your own `class` for a matrix and make it suitable for your needs. – Dima Maligin Oct 03 '15 at 20:34
  • @EJ look carefully at the matrix, it has `12` rows and up to `31` columns, you're doing a completely opposite loop. Just look at the loop in my code, and see how it takes care of the different row lengths int the condition: `day < months[month].length`. – Óscar López Oct 03 '15 at 22:02