0

I have an array that when printed, prints like this:

    W V E R T I C A L L 

    R O O A F F L S A B 

    A C R I L I A T O A 

    N D O D K O N W D C 

    D R K E S O O D D K 

    O E E P Z E G L I W 

    M S I I H O A E R A 

    A L R K R R I R E R 

    K O D I D E D R C D 

    H E L W S L E U T H

How can I get it to print like this:

    # # # # # # # # # # # #

    # W V E R T I C A L L #

    # R O O A F F L S A B #

    # A C R I L I A T O A #

    # N D O D K O N W D C #

    # D R K E S O O D D K #

    # O E E P Z E G L I W #

    # M S I I H O A E R A #

    # A L R K R R I R E R #

    # K O D I D E D R C D #

    # H E L W S L E U T H #

    # # # # # # # # # # # #

I have tried making the size larger to accommodate for the extra characters but I keep getting exception out of bounds. I am basically trying to get a non letter character to make a border around my array so that when I implement my crossword solver, when searching for a letter in the surrounding letters, it doesn't hit the boundary, instead hits the non letter character and then passes false.

This is my code that builds my array:

    for (int i=1; i<row; i++){
        String getChar = (new String(sc.next()));
        for(int j = 1;j<col; j++){
            puzzle[i][j] = getChar.charAt(j);
            }
        }

Any help is appreciated!

EDIT: This prints my array:

     for (int i=0; i<puzzle.length; i++){

           System.out.print(Arrays.toString(puzzle[i]));
           System.out.println("");
     }

EDIT2: This is my check method:

    private static boolean checkNE(int row, int col, String word, char[][] puzzle) {
    //Checking diagonals direction
    for(int letter = 1; letter < word.length(); letter++) {
        if(puzzle[row - letter][col + letter] != word.charAt(letter)) {
            return false;
        }
    }
    return true;
}
4
  • And the code that prints your array? Oct 1, 2013 at 16:40
  • 4
    What about instead of storing the #s in the array, just print out a row of #s before and after printing the array, and on each row just start and end with a #?
    – jonhopkins
    Oct 1, 2013 at 16:41
  • The code that prints my array is: ` for (int i=0; i<puzzle.length; i++){ System.out.print(Arrays.toString(puzzle[i])); System.out.println(""); }`
    – Drieke
    Oct 1, 2013 at 16:44
  • You've tried making the size larger--the size of what? You shouldn't need to make any changes to your array in order to print it like you want. In fact, you shouldn't need to create an array at all that contains '#' characters. Just use System.out.print("#") or something similar, at the appropriate times.
    – ajb
    Oct 1, 2013 at 16:44

7 Answers 7

4

Instead of making a border, just check to see if the position you're about to check is outside of the array, and if it, then you know you won't find the whole word as we would have to go outside of the array.

For example, you're array is n x n, if you're trying to access position n then it'll be out of bounds, or if you try to access a position less than 0 it will also be out of bounds. So just make sure when you're accessing it's within this range.

1
  • How would I go about doing that with my current check method. I edited it into the post at the bottom. Can I add an extra conditional in my if statement?
    – Drieke
    Oct 1, 2013 at 17:14
2

You should probably be using a class, or bounds checking but here's how to do what you want to do. Assuming your crossword is row rows and col columns, you need puzzle to be of type char[row+2][col+2].

String getChar = '';
for (int i=0; i<row+2; i++){
    if (i > 0 && i < row +1) {
       getChar = (new String(sc.next()));
    }
    for(int j = 0;j<col+2; j++){
        if (i % (row + 1) == 0 || i % (col + 1) == 0) {
            puzzle[i][j] = '#';
        else {
            puzzle[i][j] = getChar.charAt(j-1);
        }
    }
}

Check it out here on ideone.com, if you want to see it in action.

You asked about doing it with row and col pre incremented, it looks something like this:

for (int i=0; i<row; i++){
    if (i > 0 && i < row +1) {
       getChar = (new String(sc.next()));
    }
    for(int j = 0;j<col; j++){
        if (i % (row - 1) == 0 || j % (col - 1) == 0) {
            puzzle2[i][j] = '#';
        } else {
            puzzle2[i][j] = getChar.charAt(j-1);
        }
    }
}
4
  • If I add 2 to row and col before this loop, can It work with just i<row and j<col? or must they be i<row+2 and j<col+2 even after increasing the size. Because I still get java.lang.ArrayIndexOutOfBoundsException: 12.
    – Drieke
    Oct 1, 2013 at 17:04
  • 1
    Your array must be of size [row+2][col+2] if you are inserting these into the actual data of size [row][col]. You must initialize the array larger or arrayCopy to an appropriate sized one.
    – Eric
    Oct 1, 2013 at 17:27
  • Uh... if you add two to col and row before the loop, then the 'if' should be with row-1 and col-1... Oct 1, 2013 at 17:27
  • I've added a link of it in action and fixed some minor arithmetic errors, if you're still interested. Oct 1, 2013 at 17:57
1
for (int i=1; i<row; i++){
    String getChar = (new String(sc.next()));
    for(int j = 1;j<col; j++){
        puzzle[i][j] = getChar.charAt(j);
    }
}

BTW: Remember that indexes in Java starts from 0 (not 1). In your example you iterate from 1 to maximum column length, which is why you can get IndexOutOfBoundsException

0

I'm not a Java programmer, but I thought Java array indexing starts at zero, like C. So, if you make puzzle to be 10x10, and then try to access puzzle[1][10], you will get an "exception: out of bounds" error. That is, puzzle doesn't go from puzzle[1][1] to puzzle[10][10]. It goes from puzzle[0][0] to puzzle[9][9]. If this is incorrect, Java programmers please correct.

0

You need to use nested loops. Firstable you are not printing anything in your current code like the start of the array index is 1, that way you will never put anything to the first element which index is 0.

public class BorderArray {
  public static void main(String[] args) {
    // Setup the puzzle, this may change or use input.
    //Assert that this string is 100 size length or row/col will not work.
    String chars = "WVERTICALLROOAFFLSABACRILIATOANDODKONWDCDRKESOODDKOEEPZEGLIWMSIIHOAERAALRKRRIRERKODIDEDRCDHELWSLEUTH";
    int row = 10, col = 10;
    char puzzle[][] = new char[row][col];
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        puzzle[i][j] = chars.charAt(i + j);
      }
    }
    // You print the top border.
    // # # # # # # # # # # # # ... print '#' col+2 times
    for (int j = 0; j <= col; j++) {
      System.out.print("# "); // inline
    }
    System.out.println("# "); // move to next line

    // # W V E R T I C A L L # ... print '#' before and after the characters of the row
    // start i at 0 since arrays start at 0
    for (int i = 0; i < row; i++) {
      // for every row print chars of every column inline
      System.out.print("# "); // before
      for (int j = 0; j < col; j++) {
        System.out.print(puzzle[i][j] + " "); // print every char
      }
      System.out.println("#"); // after and next line
    }

    // Bottom border
    // # # # # # # # # # # # # ... print '#' col+2 times, you don't need new line since this is the end.
    // Just use col+1 as limit of the loop.
    for (int j = 0; j <= col + 1; j++) {
      System.out.print("# ");
    }

  }
}
0

Simple, don't build it into the array. Its not your actual data anyways, it's your "presentation layer".

private void printFullBorder(int col) { 
    for (int x=0; x<col+2; x++) {  //+2 because there are the leftmost + rightmost to account for
        System.out.print("#");  //unclear if this should be "#" or " #" based on your formatting, but whatever.
    }
}

printFullBorder(col);
for (int i=0; i<puzzle.length; i++){
    System.out.print("#" + Arrays.toString(puzzle[i]) + "#");
    System.out.println("");
}
printFullBorder(col);
3
  • I need to build it into my array so my check methods can see the "#" and return a false instead of just throwing an out of bounds exception.
    – Drieke
    Oct 1, 2013 at 17:07
  • If the only data inside your 2-dim array is actual data, you can treat any ArrayIndexOutOfBoundsException as garbage input or collision to a boundary and forget about it. Instead of having to test for this "magic character" that has infiltrated your data AS WELL AS deal with the possible garbage input exception.
    – Eric
    Oct 1, 2013 at 17:23
  • 2
    @Drieke You confused a lot of us when you said "How can I get it to print like this", when your question wasn't really about the appearance of the array when you printed it.
    – ajb
    Oct 1, 2013 at 18:40
0
public static String[] addBorder(String picture[]) {
    String[] border = new String[picture.length + 2];
    String pattern = "";
    for (int i = 0; i <= picture[0].length() + 1; i++) {
        pattern = pattern.concat("#");
    }
    border[0] = pattern;
    border[border.length - 1] = pattern;

    for (int i = 1; i <= border.length - 2; i++) {
        border[i] = "#" + picture[i - 1] + "#";
    }

    return border;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.