3 Replies - 1118 Views - Last Post: 04 January 2012 - 01:53 AM Rate Topic: -----

#1 noobkillerKJ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 09-August 11

How do use 2D arrays to display numbers in a box format?

Posted 04 January 2012 - 01:37 AM

I have to make an output exactly like this

123
456
789

using 2D arrays.

This is what I have so far but all I'm getting is 123456789

// The "TwoDArray" class.
import java.awt.*;
import hsa.Console;

public class TwoDArray
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();

        int array[] [] = new int [3] [3];
        array [0] [0] = 1;
        array [0] [1] = 2;
        array [0] [2] = 3;
        array [1] [0] = 4;
        array [1] [1] = 5;
        array [1] [2] = 6;
        array [2] [0] = 7;
        array [2] [1] = 8;
        array [2] [2] = 9;

        for (int x = 0 ; x < array.length ; x++)
        {
            for (int y = 0 ; y < array [0].length ; y++)
                c.println (array [x] [y]);

        }


    } // main method
} // TwoDArray class





yes I know I'm using non-standard classes but this is how I was taught.

Is This A Good Question/Topic? 0
  • +

Replies To: How do use 2D arrays to display numbers in a box format?

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: How do use 2D arrays to display numbers in a box format?

Posted 04 January 2012 - 01:46 AM

I dont expect to print that way because you used println(). I was expecting to print every element on its line. By the way if those methods are the same as standard print methods, then after every one loop of outer loop, you need to got to the next line like:
        for (int x = 0 ; x < array.length ; x++)
        {
            for (int y = 0 ; y < array [x].length ; y++)
                c.print(array[x][y]);
            c.println();

        }

This post has been edited by smohd: 04 January 2012 - 02:19 AM

Was This Post Helpful? 1
  • +
  • -

#3 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: How do use 2D arrays to display numbers in a box format?

Posted 04 January 2012 - 01:53 AM

Let's go through your loop first, to see what's happening

for (int x = 0 ; x < array.length ; x++)
{
	for (int y = 0 ; y < array [0].length ; y++)
	c.println (array [x] [y]);
}



So, suppose our outer loop is at 0, using the "normal" System.out.println() then it will start incrementing your innerloop, resulting in this:

//Output
1
2
3



(assuming println() is equal to my earlier proposed System.out.println()
Then outerloop increments again, till it reaches the end. As each increment, either by the inner- or the outerloop results in a new line, you will keep this result.

But that's not what you want, so how do we solve this? We want that each number is printed after each other, then a new line should follow up when the outer loop increments and printing the numbers after each other again. Repeat till the outer loop cannot increment anymore.

Well basically in pseudocode, it should look like this:
for (int i = 0; i < array.length(); i++)
{
  for (int j = 0; j< array[0].length(); j++)
     //Print the numbers after each other (Use System.out.print() for example)
  
  //Print a new line (Use System.out.println() for example)
}



This should give you a hint about how to do it ;)

Edit ninja'd :shuriken:

This post has been edited by karabasf: 04 January 2012 - 01:54 AM

Was This Post Helpful? 1
  • +
  • -

#4 noobkillerKJ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 09-August 11

Re: How do use 2D arrays to display numbers in a box format?

Posted 04 January 2012 - 01:53 AM

View Postsmohd, on 04 January 2012 - 01:46 AM, said:

I dont expect to print that way because you used println(). I was expecting to print every element on its line. By the way if those methods are the same as standard print methods, then after every one loop of outer loop, you need to got to the next line like:
        for (int x = 0 ; x < array.length ; x++)
        {
            for (int y = 0 ; y < array [0].length ; y++)
                c.print(array[x][y]);
            c.print();

        }


Hey thanks you helped! I forgot to put c.println(); after the last column in the row.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1