Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How do I print my 2d array?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How do I print my 2d array?

    I am making a hotel booking system and can only take bookings in the month of April. I am having trouble creating the booking calendar and am using a 2d array for the month April I am not sure if this is a good way or not if so could somebody advise me. I want to print the array out so it has the dates like so:

    1,2,3,4,5,6,7
    8,9,10,11,12,13,14
    15,16,17,18,19,20,21
    22,23,24,25,26,27,28
    29,30


    Here is my code:

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package ofi;

    /**
    *
    * @author Adam2_000
    */
    public class BookingCalendar {


    public static void main(String[] args) {


    int[][] april = new int[5][7];

    //First Row

    april[0][0] = 1;
    april[0][1] = 2;
    april[0][2] = 3;
    april[0][3] = 4;
    april[0][4] = 5;
    april[0][5] = 6;
    april[0][6] = 7;

    //Second Row

    april[1][0] = 8;
    april[1][1] = 9;
    april[1][2] = 10;
    april[1][3] = 11;
    april[1][4] = 12;
    april[1][5] = 13;
    april[1][6] = 14;

    //Third Row

    april[2][0] = 15;
    april[2][1] = 16;
    april[2][2] = 17;
    april[2][3] = 18;
    april[2][4] = 19;
    april[2][5] = 20;
    april[2][6] = 21;

    //Fourth Row

    april[3][0] = 22;
    april[3][1] = 23;
    april[3][2] = 24;
    april[3][3] = 25;
    april[3][4] = 26;
    april[3][5] = 27;
    april[3][6] = 28;

    //Fifth Row

    april[4][0] = 29;
    april[4][1] = 30;



    System.out.print(april);
    }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do I print my 2d array?

    So you want to print a month. "like this" you say...

    Well then what you really want to do is print the weeks of the month

    Except that is not good enough, what you really want to do is print the days.

    Except that is not good enough, what you really want to do is print up to 7 days on a row, and if there are more than 7 days make a new row.

    So while there are less than 7 days on the current row, and there are days in the month left to print, print a day on the current row.

Similar Threads

  1. Applet array paint/print question
    By allhuber in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 30th, 2012, 08:42 AM
  2. Print array with commas
    By GoPro in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 27th, 2012, 12:41 AM
  3. Replies: 4
    Last Post: December 19th, 2011, 09:57 PM
  4. HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 3rd, 2011, 03:44 PM
  5. Replies: 1
    Last Post: March 22nd, 2010, 04:34 PM

Tags for this Thread