DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004

    Need some help with Java App

    I am writing a program to first create a 2D Array that has 44 Rows and 44 Columns.

    That part I got down.

    I have put in the data that needs to be in the array but I have trouble displaying it on the screen. I am not using any JFrames but rather the command screen.

    I want to display each row and column in a grid format.
    ex (it should be a box)
    | |
    | |
    | |

    What I am creating is a grid for a project I am working for in AI. This is my representation and it seemed easy to do it this. However, I do not know how to make java print a row of the array then stop at the end of that row and move down to the next row.

    If someone could post some hints or a tut I can go to that would be greatly appreciated as well. This is for a homework assignment however, the assignment is to write the AI not representation. That part is easier than this. Any help would be appreciated.

  2. #2
    Join Date
    Mar 2004
    Like this?

        for (int r=0; r<44; r++)
            for(int c=0; c<44;c++)
                System.out.println(array[r][c]+" ");

  3. #3
    Join Date
    Feb 2004
    you're also going to have to format the table to make sure the rows all line up. If each piece of data that you're printing can be a different length (such as a multiplication table where '1' is shorter than '100') then you're going to have to make sure each column in the table is at least wide enough to fit the largest piece of data. If you don't then the columns won't line up at the end.

  4. #4
    Join Date
    Feb 2004
    heres an easy trick for lining up strings:

    String myLinedUpString = " " + myInt;
    myLinedUpString = myLinedUpString.substring(myLinedUpString.length()-NUM_COLUMNS);

    so if you want 3 columns, set NUM_COLUMNS to 3..

    and youll get:

      2 ...
     11 ...
     99 ...
    100 ...
    naturally that line of spaces " " must be at least as long as the number of columns, less one

    heres how to print a table for an X x Y array:

    method called horizontalRule()
    //print the horizontal line
    for as many counts as there are in array[x] dimension
      print "+------"
    //finish the lineheader
    println "+"
    method called cellDeadSpace()
    //print the vertical bars and spaces
    for as many counts as there are in array[x] dimension
      print "|      " 
    //finish the line
    println "|"
    method called printTable(array[x][y])
    //print the data:
    for each item in array[x]
     print a line of data (for a fixed X, print the incremented Y values)
     increment x
    loop until x is exhausted
    now, just write the code
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Feb 2004
    That looks scarily like my first ever java assignment cjard! The code had to format and print out any multiplication table from 1x to 12x table.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center