5 Replies - 1316 Views - Last Post: 14 November 2012 - 02:49 PM Rate Topic: -----

#1 dunnage88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-November 12

How do I print my 2d array?

Posted 14 November 2012 - 02:08 PM

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);
}
}

This post has been edited by jon.kiparsky: 14 November 2012 - 02:17 PM
Reason for edit:: added code tags - please use code tags and format your code nicely!


Is This A Good Question/Topic? 0
  • +

Replies To: How do I print my 2d array?

#2 WolverineX   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 11-November 12

Re: How do I print my 2d array?

Posted 14 November 2012 - 02:19 PM

This is the algorithm to print 2D Arrays.

for (int i =0; i <= 3; i++) {
for (int j = 0; j <= 6; j++) {
System.out.print(" " + april[i][j]);
}
System.out.println("");
}

And for the fourth row

System.out.println(april[4][0] + " " + april[4][1]);
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: How do I print my 2d array?

Posted 14 November 2012 - 02:26 PM

Java includes some syntactic sugar to print some vanilla arrays, but not 2D arrays. Frankly, it's easier to just loop through the array and print it yourself than to try to remember what's going to print how. Do it as a method, so you can reuse the code. Something like this should work, but I haven't tested it. It'll give you the idea, anyway.

private String monthToString(int[][] month){
  StringBuilder sb = new StringBuilder();
  for (int[] row: month){
    for (int i: row){
      sb.append(i + ",");
    }
    sb.replace(sb.length()-1, sb.length(), "\n"); // replace last comma with newline
  }
  return sb.toString();
}


EDIT: notice that I write this to return a String rather than just printing it. This allows you to do what you like with the String - you might want to write it to a file or put it into a GUI element or something, so this gives you some flexibility.

This post has been edited by jon.kiparsky: 14 November 2012 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: How do I print my 2d array?

Posted 14 November 2012 - 02:30 PM

Why a 2d array? Can't you just do something like

       int numberOfDaysInMonth = 30;

        for (int i = 1; i <= numberOfDaysInMonth; i++) {
            System.out.printf("%3d", i);

            if ((i % 7) == 0) {
                System.out.println();
            }
        }


Was This Post Helpful? 2
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How do I print my 2d array?

Posted 14 November 2012 - 02:33 PM

I am afraid the decision of using a 2D array here is useless.
The idea of representing a month in 4 or 5 rows as in a calendar is a commodity for human beings but completly useless for a computer.
Your April array should be a single dimenion array with 30 slots.

Later on, when it will be time to print it may be you can add a \n at each % 7 == 0 or something like that
Was This Post Helpful? 1
  • +
  • -

#6 dunnage88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 14-November 12

Re: How do I print my 2d array?

Posted 14 November 2012 - 02:49 PM

Ah okay I see now thanks a lot guys that has helped greatly!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1