【问题标题】:Final loop does not iterate, even though debugger shows test is true即使调试器显示测试为真,最终循环也不会迭代
【发布时间】:2014-12-22 01:39:51
【问题描述】:

这个类中的最后一个 for 循环是罪魁祸首。我将模式字写入新创建的数组的位置。即使 eclipse 调试器显示值 i 小于 (tokens.length-2),for 循环也不会最后一次迭代。也许这是一个围栏问题,但我尝试了一个 do while 循环和一堆东西。此外,我已经发布了我正在使用的客户端代码和 txt 文件。

// This class creates an object wherein a text file is segmented and stored
// word for word in an array, facilitating a word count, the ability to check
// for the occurrence of a word and also the functionality of returning the 
// most frequently occurring words.

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class TextAnalysis14 {
    private String[] tokens;
    int maxNoOfWords;

    // Constructor that loads a file and assigns each word to an array index
    public TextAnalysis14 (String sourceFileName, int maxNoOfWords) throws FileNotFoundException{
        this.maxNoOfWords = maxNoOfWords;
        Scanner in = new Scanner(new FileReader(sourceFileName));
        String file = in.useDelimiter("\\Z").next();
        this.tokens = file.split ("[^a-zA-Z]+");
        in.close();
    }
    // Returns the number of words in the file.
    public int wordCount(){

        return tokens.length;

    }
    // Checks whether " word " is a word in the text.
    public boolean contains(String word){
        for(int i=0; i<tokens.length; i++){
            if(tokens[i].equalsIgnoreCase(word)){return true;}
        }
        return false;
    }
    // Returns the most frequent word(s) in lexicographical order.
    public String [] mostFrequentWords(){
        Arrays.sort(tokens);
        //Finds the mode word occurrence
        int wordValue=1;
        int maxValue=1;
        for(int i=0; i<tokens.length-2; i++){
            while(tokens[i].equalsIgnoreCase(tokens[i+1])){
                wordValue++;
                i++;
            }
            if(wordValue>maxValue){
                maxValue=wordValue;
            }
            wordValue=1;
        }
        //Determines length of return array 
        int numberOfModes=1;
        for(int i=0; i<tokens.length-2; i++){
            while(tokens[i].equalsIgnoreCase(tokens[i+1])){
                wordValue++;
                i++;
            }
            if(wordValue==maxValue){
                numberOfModes++;
            }
            wordValue=1;
        }
        //writes mode words to array
        int cursor =0;
        String[] modeWords = new String[numberOfModes];
        for(int i=0; i<tokens.length-2; i++){
            while(tokens[i].equalsIgnoreCase(tokens[i+1])){
                wordValue++;
                i++;
            }
            if(wordValue==maxValue){
                modeWords[cursor]=tokens[i];
                cursor++;
            }
            wordValue=1;
        }
        return modeWords;
    }

}

以下是我的客户端代码:

import java.io.FileNotFoundException;
import java.util.Arrays;

public class TextAnalysis_test01 {

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

        TextAnalysis14 ta14 = new TextAnalysis14("testtext01.txt", 100);
        System.out.println(ta14.wordCount());
        System.out.println(ta14.contains("Bla"));
        System.out.println(ta14.contains("hello"));
        System.out.println(Arrays.toString(ta14.mostFrequentWords()));


    }

}

以下是我的txt文件内容:

bla bla
dim dim 
dum dum

我得到了输出:

6
true
false
[bla, dim, null]

据我所知,很明显,我没有向返回的字符串数组中的最后一个索引写入任何内容,因为最终的 for 循环没有最后一次迭代。我班上的部分评论为://将模式字写入数组。

任何帮助或建议都是天赐之物。干杯!

【问题讨论】:

    标签: java loops fencepost


    【解决方案1】:

    我真的建议您重构代码以尝试改进它,但如果您只是在修复之后,以下更改应该会产生预期的输出:

    int cursor =0;
        String[] modeWords = new String[numberOfModes];
        for(int i=0; i<=tokens.length-2; i++){
            while(i+1 <= tokens.length-1 && tokens[i].equalsIgnoreCase(tokens[i+1])){
                wordValue++;
                i++;
            }
            if(wordValue==maxValue){
                modeWords[cursor]=tokens[i];
                cursor++;
            }
            wordValue=1;
        }
        return modeWords;
    }
    

    【讨论】:

    • 您能解释一下如何纠正这种行为吗?我并不是说它错了,我没有尝试过,但是代码不是自我解释的,所以我不认为代码本身就是一个答案。
    • 正如我在答案中所说,整个代码有点混乱,因此我强调了重构的迫切需要。为了可读性,肯定有更好的方法来实现他的愿望。关于解释,更改允许访问数组的最后 2 个位置:为此,我将 for 循环的条件从 i
    • 更准确地说是 ArrayIndexOutOfBoundsException。这就是为什么我添加了条件的“i+1
    • 确实做到了。但我绝对不保证这将适用于任何其他输入值,我只更改了他写入数组的部分,这就是所要求的。
    • 我猜在嵌套循环中使用相同的变量是不好的做法?
    猜你喜欢
    • 2018-06-14
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    相关资源
    最近更新 更多