简体   繁体   中英

Trying to create a word search using a 2D Array and user input to set the size?

I am trying to get a user to input the size of a word search puzzle but whenever i run the program I get array out of bound error. Example, say I enter 10, then the puzzle would be a 10 by 10 word search. I am fairly new to programming and want to create this using objects and instance instance variables.

import java.util.Scanner;

public class Puzzle {


private char [][] myPuzzle;// creating my object



 public static void main(String[] args) {
     Puzzle myPuzzle = new Puzzle(); // creating an instance of my puzzle object
     myPuzzle.puzzleSize();

 }

 public char[][] puzzleSize(){
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Hi, What would you like your puzzle size to be?");
     int puzzleSize = keyboard.nextInt();    
     int row = puzzleSize;
     int col = puzzleSize;
     char[][] puzzle= new char [row][col];

      for(int i=0;i<puzzle.length;row++){ // creating the puzzle size

        for(int o=0;o<puzzle.length;col++){
            System.out.print(puzzle[row][col]);
        }
        System.out.println("");
    }

     return puzzle;
  }
}

System.out.print(puzzle[row][col]); should be replaced with System.out.print(puzzle[i][o]);

Try to change this line,

for(int i=0;i<puzzle.length;row++){
    for(int o=0;o<puzzle.length;col++){
        // iterate column
        System.out.print(puzzle[row][col]);

with,

for(int i=0;i<puzzle.length;i++){
    for(int o=0;o<puzzle[i].length;o++){..
        System.out.print(puzzle[i][o]);
  for(int i=0;i<puzzle.length;row++){ // creating the puzzle size

    for(int o=0;o<puzzle.length;col++){
        System.out.print(puzzle[row][col]);
    }
    System.out.println("");
  }

Couple issues here.

Each of your for loops aren't properly built. Lets take a look at your first loop:

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

Your intent is to increment the integer i for each pass of the loop, until i is the length of the array. What you're actually doing is setting i=0, and for each pass, incrementing the row integer, causing your loop to continue indefinitely. Fix this with the following change:

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

You're doing the same with the second loop. Change col++ to o++ when building your for-loop.

Another problem with your second loop is that you're checking puzzle.length as your paramater for max length. You want to be checking the length of the array's second dimension for each value of the first dimension given the value of i from the first loop:

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

Now that the loops are working properly, you need to fix your print value to properly print the value of each current array element:

System.out.print(puzzle[i][o]);

Instead of using row and col, which were only used during the creation of the array to indicate the array size.

Also keep in mind that assigning two additional integers row and col is redundant as you already have puzzleSize assigned to the value you want:

char[][] puzzle = new char[puzzleSize][puzzleSize]

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM