Hello. I am trying to do a search for a keyword in a file, but my output that is just coming up is "Processing file". It won't say that it's actually finding the keyword in a file.
As can be seen, I have used a preset keyword of forensics and i have told my program to look for that in a .txt file.
The problem I am getting is that it only says "Processing file"; the output won't show that any keyword has been found, which is the main aim of the project.
I can't work out where I am going wrong with this, any help would be greatly appreciated.
    package filelistingvisitor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;
public final class FileListingVisitor {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        String ROOT = "F:\\";
        FileVisitor<Path> fileProcessor = new ProcessFile();
        Files.walkFileTree(Paths.get(ROOT), fileProcessor);
    }
    private static final class ProcessFile extends SimpleFileVisitor<Path> {
        @Override
        public FileVisitResult visitFile(
                Path aFile, BasicFileAttributes aAttrs) throws IOException {
            System.out.println("Processing file:" + aFile);
            String fileName = aFile.toString();
            int nameLength = fileName.length() - 4;
            if (fileName.substring(nameLength, nameLength + 4) == ".txt") {
                fileScan(aFile);
            }
            return FileVisitResult.CONTINUE;
        }
        @Override
        public FileVisitResult visitFileFailed(
                Path file, IOException e) throws IOException {
            System.err.printf("Visiting failed for %s\n", file);
            return FileVisitResult.SKIP_SUBTREE;
        }
        @Override
        public FileVisitResult preVisitDirectory(
                Path aDir, BasicFileAttributes aAttrs) throws IOException {
            System.out.println("Processing directory:" + aDir);
            return FileVisitResult.CONTINUE;
        }
    }
    public static void fileScan(Path aFile) throws FileNotFoundException, IOException {
        String searchterm = "forensics" ;
        Scanner scanner = new Scanner(aFile);
        while (scanner.hasNextLine()) {
            String nextToken = scanner.next();
            if (nextToken.equalsIgnoreCase(searchterm)) {
               System.out.println("Phrase Found" + searchterm + "in file" + aFile);
               break;
            }
        }
    }
}
 
                        
As claymore mentioned replacing:
with:
is a lot cleaner. You may still run into
NoSuchElementExceptionwhenfileScanis called. Modify the method like so:This will read a full line of the file and then check if "forensics" is contained in that line. Alternatively, you could replace:
with:
in your original method.