Question

this snippet of code works fine on my development machine (Windows 7 installed on VirtualBox jre 8 using Netbeans IDE), but on another machine (Windows 7 jre 8) always returns true. It should only find files with a name like "town_house.html" instead it always returns true for every file in the folder.Running the jar file from prompt I don't get any exceptions. Maybe it's just a trivial error I usually program in C/C++ ... any idea?

for(File f : files)
        {
            if(f.toString().contains("_") && 
               f.toString().contains(".html")){
                System.out.print("Processing file: " + f.getName()+ "\n");
                String[] fileSplit = f.getName().split("_");
                towns.add(fileSplit[0]);

            }
        }

Thanks in advance

Was it helpful?

Solution

You are checking toString() instead of getName() - maybe the directory path contains an underscore.

Try this instead (note also simplified test):

for(File f : files) {
    if (f.getName().matches(".*_.*\\.html")) {
        System.out.print("Processing file: " + f.getName()+ "\n");
        String[] fileSplit = f.getName().split("_");
        towns.add(fileSplit[0]);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top