Search code examples
javaarraysmultidimensional-arrayuser-input

put user inputted word inside 2d array in java


I'm having a real difficult time trying to enter user's inputted words inside the 2d array. What my program needs to do is create a word search puzzle that prompts user for how many words he or she will want to find, and what the given words are going to be. What I'm having trouble with is that I can't seem to put the user's input inside the 2d array. Below is my current code:

 public static void generate(){
      int rows = 5;
      int columns = 5;
      char[][] table = new char [rows][columns];
      int numberwanted;

      System.out.println("Type in the number of words you want to generate: ");
      numberwanted = userinput.nextInt();
      System.out.println("Type in the words you want to generate: ");
      for (int i = 0; i < numberwanted; i++){
         String words = userinput.next();
         char te = words.charAt(i);
         for(int r = 0; r < rows; r++){
            for(int c = 0; c < columns; c++){
               table[r][c] = te;
               System.out.print(table[r][c] + " ");
            }
            System.out.println();
         }

      }// forloop   

The output:

Type in the number of words you want to generate: 
2
Type in the words you want to generate: 
test
t t t t t 
t t t t t 
t t t t t 
t t t t t 
t t t t t 

goal output:

Type in the number of words you want to generate: 
2
Type in the words you want to generate: 
test
hi
t e s t x          x x t x x
x x x x x    or    x x e x x   and so on... with x's being empty
x h x x x          x x s x x 
x i x x x          x x t x x
x x x x x          x x h i x 

Solution

  • I tweaked the whole program a bit to make it more robust. How it works now:

    • process each row of the table.
    • if the user wanted to provide a word, ask for the word.
    • add the word (perhaps truncated) to the current row.
    • right pad the row with x if needed.

    At last, render the whole table.

    private static int ROWS = 5;
    private static int COLUMNS = 5;
    
    public static void main(String[] args) {
        char[][] table = new char[ROWS][COLUMNS];
        int numberwanted;
        try (Scanner in = new Scanner(System.in)) {
            System.out.println("Type in the number of words you want to generate: ");
            numberwanted = Math.min(in.nextInt(), ROWS);
    
            for (int row = 0; row < ROWS; row++) {
                String word = null;
                if (row < numberwanted) {
                    System.out.println(String.format("Type in the %d. word: ", row));
                    word = in.next();
                }
                for (int col = 0; col < COLUMNS; col++) {
                    table[row][col] = word != null && word.length() > col ? word.charAt(col) : 'x';
                }
            }
        }
        renderGrid(table);
    }
    
    private static void renderGrid(char[][] grid) {
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLUMNS; col++) {
                System.out.print(grid[row][col] + " ");
            }
            System.out.println();
        }
    }