gpt4 book ai didi

java - 编译代码但不执行Java

转载 作者:行者123 更新时间:2023-12-01 15:00:57 25 4
gpt4 key购买 nike

我已经编写了下面的代码,据我猜测它应该正确执行,但它没有正确执行,我正在用头撞墙。找到要添加到临时 vector 列表中的对象后,我想做的是循环遍历 vector 列表,获取每个文件名并计算单词出现的次数。我可以让这两段代码单独工作,但是当我将它们放在一起时,代码在填充临时 vector 列表后停止执行。谁能从我下面的代码中看到是什么阻止了其余代码的执行?

编辑>我已经更改了代码中的一些内容以正确格式化并删除重复项,但现在没有任何内容输出到屏幕上。我确信这很简单,但我似乎看不到它。

                    for(int m = 0; m < temp.size() && occurCount < wordCount;m++)
{
File aFile2 = new File(temp.elementAt(m));
FileReader aFileReader = new FileReader(aFile2);
BufferedReader aBufferReader = new BufferedReader(aFileReader);

while((line = aBufferReader.readLine()) != null && occurCount < wordCount)
{

words = line.toLowerCase().split(" ");
if (line == null)
break;
else
{
for(int k = 0; k < words.length && occurCount < wordCount;k++)
{
if(words[k].matches(wordToSearch))
{
occur++;

q.add(words[k]);

}
else if(words[k].matches(pattern))
{
System.out.println(words[k]);

if(temp.contains(words[k]))
System.out.println("Word already in list");
else
{
temp.add(words[k]);
}

}


}
}
lineFile = "";


}
occurCount = occur;
occur = 0;
aBufferReader.close();
searched.add(temp.elementAt(m));
occurCount2 += occurCount;
//occurCount += occurCount;



System.out.println(occurCount);
System.out.println(q);
System.out.println(temp);
System.out.println(searched);

最佳答案

                char aChar = 'x';
for (int i = 0; i <= line.length()-1; i++)
{
aChar = line.charAt(i);
lineFile += aChar;
}

words = lineFile.split(" ");
if (line == null)
break;
lineFile = "";

将与:

相同
               words = line.split(" ");
<小时/>
                for(int k = 0; k <= words.length - 1;k++)
{
if(words[k].matches(pattern))
{
System.out.println(words[k]);
if(!searched.contains(words[k]))
{
if(temp.contains(words[k]))
System.out.println("Word already in list");
else
{
temp.add(words[k]);
}
}
}

通常会使用for (int i = 0; i < n; i++)而不是<= n - 1 .

您使用了 temp.add,它可能应该被 searched.add,否则是外循环继续下去:( for ... < temp.size() )

                for (String word : words)
{
if (word.matches(pattern))
{
System.out.println(word);
if (!searched.add(word))
System.out.println("Word already in list");
}
}

add当该单词已包含在集合中时返回 false。

words必须是通用类型,如 Set<String> .

关于java - 编译代码但不执行Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13658508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com