我复制了 Horstmann 书(第 2 卷)中的代码示例,但不明白为什么它不起作用。你能帮助我吗?我尝试删除 IOException,但它引发了另一个问题
package streams;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
public class Hello {
public static void main(String[] args) throws IOException
{
String contents = new String(Files.readAllBytes(Paths.get("text.txt")), StandardCharsets.UTF_8);
List<String> words = Arrays.asList(contents.split("\\PL+"));
long count = 0;
for(String w : words)
{
if (w.length() > 12) count++;
}
System.out.println(count);
count = words.stream().filter(w -> w.length() > 12).count();
System.out.println(count);
count = words.parallelStream().filter(w -> w.length() > 12).count();
System.out.println(count);
}
}
最佳答案
当你创建一个新的java类时,你是否创建了一个名为Hello的类?该错误正在清除,表明它找不到您的类,因此引发错误。尝试使用默认包重新创建另一个 java 项目,然后在该默认包中创建一个新的 hello 类。运行一个简单的 println 并查看是否有效,如果有效,然后尝试将旧代码复制到该新文件中。希望这有帮助:)
关于java - 代码有什么问题吗? Horstmann 书中的第一个代码示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41411942/