这个打印矩阵的程序中的错误在哪里?

时间:2016-03-27 19:58:01

标签: java multidimensional-array

我创建了一个矩阵程序,它接收文件中的单词并将它们水平或垂直打印出来。程序检查单词的字符是否已经在某个位置,或者该位置是否为空白以确保其正确打印出来。如果在另一个单词或字符的顶部打印一个单词或字符,则会生成一个新的点以将该单词输入。这样做直到该单词可以无错误地打印。空格填充字母“A”。

import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class WordSearch {

public WordSearch() {

}

public void getMatrix() throws FileNotFoundException {



    File file = new File("/home/cameron/Desktop/words");

    ArrayList<String> words = new ArrayList<String>();

    String largest = "";

    char[][] data;

    int col = 0;




    Scanner sc = new Scanner(file);


    while (sc.hasNextLine()) {


        String first = sc.nextLine();

        words.add(first);


        if(first.length()>largest.length()){



         largest=first;

         col=largest.length();

        }
    }



        data = new char[col][col];

        int curWord=0;


        for(int i=0; i<words.size(); i++){
            String word=words.get(curWord);  
            int start=0;
            int row=0;
            int choice=0;
            int index=0;


         do
         {

           start = (int)(Math.random()*data.length);
           row =  (int)(Math.random()*data.length);
           choice = (int)(Math.random()*2);

         }
         while(start+word.length()>data.length);



         index=0;

         if(choice==0){
             for(int j=start; j<start+word.length(); j++){              
                 if(data[row][j]==0 || data[row][j]==word.charAt(index)){
                     data[row][j] = word.charAt(index);

                     index++;
                 }

                 else{ i=0; continue;}


             }
         }

         if(choice==1){
             for(int j=start; j<start+word.length(); j++){
                 if(data[j][row]==0 || data[j][row]==word.charAt(index)){
                    data[j][row] = word.charAt(index);

                    index++;
                 }

                 else{ i=0; continue;}
             }
         }

           curWord++;
          }



       for(int i=0; i<data.length; i++){
         for(int j=0; j<data[i].length; j++){
            if(data[i][j]==0)
             System.out.print("A ");
            else    
             System.out.print(data[i][j]+" ");
         }

         System.out.println();
        }


    }


 public static void main(String[] args) throws FileNotFoundException {



    WordSearch check = new WordSearch();

    check.getMatrix();

 }
}  

我设置if else声明,如果单词的字符打印在不同的字符或单词之上,那么for loopdo while之上的那个)就会重新启动,以确保正确打印出该单词。

但是,我遇到了一个问题。该程序似乎工作约50%,并打印出这样的东西:

A A h A A A A f A 
A A a A A l A e A 
A A m A A a A n A 
A A b A A d A c A 
A A u A A y A e A 
A A r A A b A A A 
A A g A A u A A A 
A A e A A g A A A 
A A r A A A A A A 

偶尔我会收到此错误并仅出现此错误(在此特定索引处):

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at WordSearch.getMatrix(WordSearch.java:59)
at WordSearch.main(WordSearch.java:132)

可能是什么问题?

编辑:以下是从文件中读入程序的内容:

ladybug
hamburger
fence

1 个答案:

答案 0 :(得分:2)

好的,我通过将for循环中的公共逻辑提取到一个可读性和重用方法中来清理你的代码。此外,其他条件导致您出现问题,并且通过重置&#39; i&#39;回到0并偶尔出错。

请尝试修改此代码,如果您继续收到错误,请告知我们。连续运行约20次后,我不再看到问题:

    import java.util.*;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class WordSearch {
        ArrayList<String> words = new ArrayList<>();

        private void getMatrix(String filePath) throws FileNotFoundException {
            File file = new File(filePath);
            String largest = "";
            char[][] data;
            int col = 0;
            Scanner sc = new Scanner(file);

            while (sc.hasNextLine()) {
                String first = sc.nextLine();
                words.add(first);

                if(first.length() > largest.length()){
                    largest = first;
                    col = largest.length();
                }
            }

            data = new char[col][col];
            int curWord = 0;

            for(int i = 0; i < words.size(); i++){
                String word = words.get(curWord);
                int start = 0;
                int row = 0;
                int choice = 0;

                do{
                    start = (int)(Math.random()*data.length);
                    row =  (int)(Math.random()*data.length);
                    choice = (int)(Math.random()*2);
                }
                while(start + word.length() > data.length);

                updateData(choice, start, row, word, data);
                curWord++;
            }

            for (char[] aData : data) {
                for (char anAData : aData)
                    if (anAData == 0)
                        System.out.print("A ");
                    else
                        System.out.print(anAData + " ");
                System.out.println();
            }
        }

        private void updateData(int choice, int start, int row, String word, char[][] data){

            for(int index = 0, j = start; j < start + word.length(); j++){
                switch(choice){
                    case 0:
                        if(data[row][j] == 0 || data[row][j] == word.charAt(index)){
                            data[row][j] = word.charAt(index);
                            index++;
                        }
                        break;
                    case 1:
                        if(data[j][row] == 0 || data[j][row] == word.charAt(index)){
                            data[j][row] = word.charAt(index);
                            index++;
                        }
                        break;
                }
            }
        }

        private String getRandomLetter(){
            Random r = new Random();
            char c = (char) (r.nextInt(26) + 'a');
            return ("" + c).toUpperCase();
        }

        public static void main(String[] args) throws FileNotFoundException {
            WordSearch check = new WordSearch();
            check.getMatrix("/home/cameron/Desktop/words");
        }
    }  

该计划非常整洁,干得好。为了使它更有趣,我添加/更改了一些你可以根据自己的喜好使用或不使用的东西。

注意我添加了getRandomLetter()方法。这样你就可以使用随机字母而不仅仅是&#39; A&#39;每次填写矩阵,看起来更像真正的单词搜索。我还通过更改将文件中的单词添加到单词列表中的行来更新原始单词和每个随机字母:

words.add(first.toUpperCase());

现在输出如下:

K M D X H A L B H 
C M L G A W N F F 
M V W Z M D T O T 
L A D Y B U G T Y 
Y J U J U R D V C 
C I C O R P P I L 
B B P R G R Q L X 
D B D V E X R V K 
E I J H R G L D B