Results 1 to 4 of 4
  1. #1
    LuluMM is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 2D array of StringBuffer

    I previously posted a thread about this, but I need more help.

    *I posted my code just in case, but I briefly described what the code below does (after the code)*

    Java Code:
    import java.util.Scanner;
    
    class Board{
      static Scanner input = new Scanner(System.in);
      static String line = input.next();
      static int iRows = Integer.parseInt(line);
      
      static int column;
      static int row=iRows;
      
      
      static int num =1;
      public Board(int x, int y){
        column=y;
        row=x;
      }
      
      public static void main(String[] args){
        
        
        Board myboard=new Board(6, iRows);
        myboard.printNum();
        myboard.printBoard();
     
        StringBuffer[][]ab=new StringBuffer[row][column];
        for(int i=0; i<row;i++){
          for(int j=0; j<column;j++){
            ab[i][j]=new StringBuffer("");
          }
        }
      }
    //the top line of the board where number prints
      void printNum(){ 
        System.out.print("  ");
        for(int i=0;i<column;i++){
          for(int j=0; j<9;j++){
            System.out.print(num);
          }
          num+=1;
          System.out.print(" ");
        }
        System.out.println();
      }  
    //the top line 
      void printTop(){
        System.out.print(" +");
        for(int i=0;i<column;i++){
          for(int j=0; j<9;j++){
            System.out.print("-");
          }
          System.out.print("+");
        }
        System.out.println();
      }
    //the mid line, store it in the method for reuse it later
      void printMid(int height, int rowNum){
        for(int i=0;i<height;i++){
          System.out.print(rowNum+"|");
          for(int j=0; j<column;j++){
            for(int k=0; k<9/*-ab.length()*/;k++){
              //ab.  
              //9-ab.length how many space I print instead of k smaller than 9, it will be k is smaller than 9-ab.length
              //ab[rowNum][]
              System.out.print(" ");
            }
            System.out.print("|");
          }
          System.out.println();
        }
      }
      void printBoard(){
        printTop();
        for(int i=0; i<row;i++){
          printMid(3, i+1);
          printTop();
          
        }
      }
    }

    My code takes in a number from the user and creates that number of columns. So if you entered four, it would print out something like this (it has four columns, and the number of rows is always the same):

    Java Code:
      111111111 222222222 333333333 444444444
      +--------+--------+---------+---------+
     1|        |        |         |         |
     1|        |        |         |         |
     1|        |        |         |         |
      +--------+--------+---------+---------+
     2|        |        |         |         |
     2|        |        |         |         |
     2|        |        |         |         |
      +--------+--------+---------+---------+
     3|        |        |         |         |
     3|        |        |         |         |
     3|        |        |         |         |
      +--------+--------+---------+---------+
     4|        |        |         |         |
     4|        |        |         |         |
     4|        |        |         |         |
      +--------+--------+---------+---------+
     5|        |        |         |         |
     5|        |        |         |         |
     5|        |        |         |         |
      +--------+--------+---------+---------+
     6|        |        |         |         |
     6|        |        |         |         |
     6|        |        |         |         |
      +--------+--------+---------+---------+

    Now, I need to use a 2d array of string buffer to keep track of each square on this board.
    So, were do I begin?

    This is all I was able to come up with:

    StringBuffer[][]ab=new StringBuffer[row][column];

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: 2D array of StringBuffer

    That just gives you an array of arrays of elements that can hold a StringBuffer reference. Next you have to iterate over the elements, probably in a nested loop, and populate those array elements with StringBuffer instances.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    LuluMM is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: 2D array of StringBuffer

    Quote Originally Posted by DarrylBurke View Post
    That just gives you an array of arrays of elements that can hold a StringBuffer reference. Next you have to iterate over the elements, probably in a nested loop, and populate those array elements with StringBuffer instances.

    db

    So I think I created what you described above. Were should I declare it/write the code? I was thinking I should do it in main, but I'm not sure.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: 2D array of StringBuffer

    Personally, since you're a beginner and doing things in an understandable way is preferable to help hone your skills, I would ditch the string buffer completely.

    Make a 2D array of either Strings or chars that is the exact size of your game board, for instance 25x25.
    Then you can keep track of character position just by knowing the x,y coordinate in the 2d array. Printing can be accomplished with a simple loop that goes row by row and collects all the chars or strings. In this way, moving a character from row to row only involves changing the value of two x,y coordinates. For example:

    Java Code:
    //Character is at (5,7), lets move him south 1 position
    map[5][8] = map[5][7]; //Copy character to the tile directly south
    map[5][7] = " "; //change the tile where the character was back to a blank space
    This makes diagonal movement, collision detection, and animation MUCH simpler.

Similar Threads

  1. StringBuffer
    By Dayanand in forum New To Java
    Replies: 9
    Last Post: 03-01-2011, 10:04 AM
  2. Replies: 3
    Last Post: 03-04-2009, 06:03 AM
  3. Replies: 1
    Last Post: 03-03-2009, 05:27 AM
  4. Should I use StringBuffer?
    By GenkiSudo in forum New To Java
    Replies: 7
    Last Post: 09-21-2008, 12:39 PM
  5. StringBuffer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •