需要帮助计算arrayList中的字符

时间:2012-04-16 11:51:54

标签: java arraylist

import java.io.* ;
import java.util.ArrayList ;
public class WordSearchPuzzle;
{
    private char[][] puzzle ;
    private ArrayList<String> puzzleWords ;
    private int letterCount = 0 ;
    private int gridDimensions;

    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    {
        this.puzzleWords = userSpecifiedWords ;

    }
    private void createPuzzleGrid()
    {
      int i;
        for(i = 0; i < puzzleWords.size(i).length ; i++){
          letterCount = puzzleWords + letterCount ;
            }
        }
        gridDimensions = letterCount * 1.5;
        puzzle[gridDimensions][gridDimensions];
    }

    public WordSearchPuzzle(String wordFile, int wordCount,
    int shortest, int longest)
    {
        // puzzle generation using words from a file
        // The user supplies the filename. In the file 
        // the words should appear one per line.
        // The wordCount specifies the number of words
        // to (randomly) select from the file for use in
        // the puzzle.
        // shortest and longest specify the shortest
        // word length to be used and longest specifies
        // the longest word length to be used.
        // SO, using the words in the file randomly select
        // wordCount words with lengths between shortest
        // and longest.

    }

    private ArrayList<String> loadWordsFromFile(String filename, int shortest, int longest)
    {
        // BasicEnglish.txt - the 850 words of Basic English
        // BNCwords.txt - "the 6,318 words with more than 800 occurrences in
        //the whole 100M-word BNC"
        try {
            FileReader aFileReader = new FileReader(filename);
            BufferedReader aBufferReader = new BufferedReader(aFileReader);
            String lineFromFile;
            int len ;
            ArrayList<String> words = new ArrayList<String>();
            lineFromFile = aBufferReader.readLine() ;
            while (lineFromFile != null) {  
                len = lineFromFile.length() ;
                if(len >= shortest && len <= longest) {
                    words.add(lineFromFile.toUpperCase());
                }
                lineFromFile = aBufferReader.readLine() ;
            }
            aBufferReader.close();
            aFileReader.close();
            return words ;
        }
        catch(IOException x)
        {
            return null ;
        }
    }

    // The dimensions of the puzzle grid should be set
    // by summing the lengths of the words being used in the
    // puzzle and multiplying the sum by 1.5 or 1.75
    // or some other (appropriate) scaling factor to
    // ensure that the grid will have enough additional
    // characters to obscure the puzzle words. Once
    // you have calculated how many characters you are
    // going to have in the grid you can calculate the
    // grid dimensions by getting the square root (rounded up)
    // of the character total.
}

嗨,小Java项目我必须在这里上大学。这是我到目前为止所拥有的。我不明白为什么它不编译。我编写了用于生成网格的代码;网格尺寸由输入字(所有输入字的字母总和* 1.5)设置。我不确定将数组列表的所有元素加在一起的部分。

发生了什么事?在此先感谢:)

3 个答案:

答案 0 :(得分:2)

在这里删除分号.... public class WordSearchPuzzle;

这不是一个声明。 puzzle[gridDimensions][gridDimensions];

for循环中的

puzzleWords.size(i).length给出了问题。如果您想要列表中的元素数量,puzzleWords.size()将起作用。然后letterCount = puzzleWords + letterCount ; ,你有不兼容的类型,ArrayList + int,你的意思是用puzzleWords.size()代替拼图吗?

答案 1 :(得分:2)

我可以看到多个问题:

在类声明行中不应有分号。

public class WordSearchPuzzle

正如Nettogrof所示,createPuzzleGrid方法中有太多}。

createPuzzleGrid中的循环使用不存在的方法。 没有size方法接受数组列表的参数。此时它也找不到字符串的长度 您在createPuzzleGrid中的循环应为:

for (int i = 0; i < puzzleWords.size; i++) {
    String item = puzzleWords.get(i);
    int itemLength = item.length();
    letterCount = letterCount + itemLength;
}

作为额外注释,该方法的最后一行访问puzzle数组但不执行任何操作,因此可以删除此行。事实上,没有方法使用puzzle变量,因此可以完全删除它。

答案 2 :(得分:1)

在你的createPuzzleGrid中,For-loop

中有两个}

correctec版本:

private void createPuzzleGrid()
{
  int i;
    for(i = 0; i < puzzleWords.size(i).length ; i++){
      letterCount = puzzleWords + letterCount ;
    }
    gridDimensions = letterCount * 1.5;
    puzzle[gridDimensions][gridDimensions];
}