Search code examples
javaexceptionstreamclassnotfoundexception

What is wrong with the code? First code example from Horstmann book


I copied the code example from Horstmann book(Volume2) and don't understand why it doesn't work. Can you help me? I tried to delete IOException, but it raises another problem

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);
}

}

Console log

enter image description here


Solution

  • Did you create a class called Hello when you were creating a new java class? The error is clearing stating that it cannot find your class, and therefore throwing the error. Try recreating another java project with a default package and just create a new hello class in that default package. Run a simple println and see if that works if so then try copying your old code into that new file. Hope this helps :)