Home > Enterprise >  How can I check if I can add word into 2D array length?
How can I check if I can add word into 2D array length?

Time:11-27

I need to check if the String word can be added on the array length, without lose any character out of bounds. Right now when I add some String words it goes out of the length and I don't know how to fix that.

Right now I fill an 2D array with (*) and I want to add words like "schedule" but when I add the word in rows and columns like 2, 5 the word goes out of bounds and print:


*relax ****sche ridiculo ******ta

And the rest characters of the word disappear.

public class WordSearch {

    private static int rows = 5;
    private static int columns = 10;
    char board[][] = new char [rows][columns];

    public WordSearch(){
        for(int row=0; row<rows; row  ){
            for(int col=0; col<columns; col  ){
                board[row][col] = '*';
            }
        }
    }

    public void addWord(String word, int position, int x , int y) {

        switch(position){
            case 0:
                for(int i=0; i<word.length(); i  ){
                    if(y   1 >= board[x].length){
                        continue;
                    } else if(board[x][y] == '*'){
                        board[x][y  ] = word.charAt(i);
                    } else {
                        board[x][y  ] = word.charAt(i);
                    }
                }
                break;
            case 1:
                for(int i=0; i<word.length(); i  ){
                    if(x   1 >= board[y].length){
                        continue;
                    } else if(board[x][y] == '*'){
                        board[x  ][y] = word.charAt(i);
                    } else {
                        board[x  ][y] = word.charAt(i);
                    }
                }
                break;
            default:
                System.out.println("Give 0 to add word horizontally, or 1 vertically");
            }
            
        }
}

CodePudding user response:

in the switch statements and its case code block,

case 0:
                for(int i=0; i<word.length(); i  ){
                    if(y   1 >= board[x].length){
                        continue;
                    } else if(board[x][y] == '*'){
                        board[x][y  ] = word.charAt(i);
                    } else {
                        board[x][y  ] = word.charAt(i);
                    }
                }
                break;

the sentence

board[x][y  ] = word.charAt(i);

after excuting it , the variable y will then continue add 1; and when excuting the next if statements , the y will add 1 again;

so you should change the if statements to if(y >= borad[x].length) like this

            case 0:
                for(int i=0; i<word.length(); i  ){
                    if(y >= board[x].length){
                        continue;
                    } else if(board[x][y] == '*'){
                        board[x][y  ] = word.charAt(i);
                    } else {
                        board[x][y  ] = word.charAt(i);
                    }
                }
                break;
            case 1:
                for(int i=0; i<word.length(); i  ){
                    if(x  >= board[y].length){
                        continue;
                    } else if(board[x][y] == '*'){
                        board[x  ][y] = word.charAt(i);
                    } else {
                        board[x  ][y] = word.charAt(i);
                    }
                }
                break;

CodePudding user response:

Try this.

public void addWord(String word, int position, int x, int y) {
    switch (position) {
    case 0:
        for (int i = 0; i < word.length() && y < columns; i  )
            board[x][y  ] = word.charAt(i);
        break;
    case 1:
        for (int i = 0; i < word.length() && x < rows; i  )
            board[x  ][y] = word.charAt(i);
        break;
    default:
        System.out.println("Give 0 to add word horizontally, or 1 vertically");
    }
}

and

addWord("abcdefghijkl", 0, 1, 1);
for (char[] row : board)
    System.out.println(Arrays.toString(row));

output:

[*, *, *, *, *, *, *, *, *, *]
[*, a, b, c, d, e, f, g, h, i]
[*, *, *, *, *, *, *, *, *, *]
[*, *, *, *, *, *, *, *, *, *]
[*, *, *, *, *, *, *, *, *, *]
  • Related