dtonhofer

dtonhofer

Functional Programming in Java, Second Edition: p.61, ListSelectFiles.java needs a "printer"

We find this code on page 61, for ListSelectFiles.java

final String[] files =
        new File("fpij").list(new java.io.FilenameFilter() {
            public boolean accept(final File dir, final String name) {
                return name.endsWith(".java");
            }
        });
System.out.println(files);

But this just prints the identifier of the array object, not the array contents.

The last line needs to be for example

System.out.println(Arrays.stream(files).collect(Collectors.joining("\n")));

The original code also NullPointerExcepts if “fpij” is nonexistent or not a directory.

Thus

    public void listSelectFiles() {
        String dir = "/home/aloy";
        final String[] files =
                new File(dir).list(new java.io.FilenameFilter() {
                    public boolean accept(final File _dir, final String name) {
                        return name.endsWith(".java");
                    }
                });
        String res =
                (files == null) ?
                        ("Looks like '" + dir + "' is not a directory") :
                        (Arrays.stream(files).collect(Collectors.joining("\n")));
        System.out.println(res);
    }

Marked As Solved

venkats

venkats

Author of Programming Kotlin, Rediscovering JavaScript (and 6 other titles)

Addressed, fixed. Thank you.

Popular Pragmatic topics Top

New
Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New
telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “>...
New
hazardco
On page 78 the following code appears: <%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New

Other popular topics Top

AstonJ
Or looking forward to? :nerd_face:
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
PragmaticBookshelf
A Ruby-Centric Chat with Noel Rappin @noelrappin Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New

Latest in PragProg

View all threads ❯