Naresh Saxena
Naresh Saxena

Reputation: 39

Error in java program related to Map

I have developed a application in which user select the particular folder and it counts all the java files in that folder plus the line of code individually in those files and show at console but in a java project there are so many packages and right now I have to navigate until a particular package , I want to modify the application in such a way that when user select the particular project, he will then further navigate to only src folder and from src folder all the packages containing java files line of code will be counted.

public class abc {

    /**
     * @param args
     * @throws FileNotFoundException
     */
    private static int totalLineCount = 0;
    private static int totalFileScannedCount = 0;

    public static void main(String[] args) throws FileNotFoundException {

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("C:" + File.separator));
        chooser.setDialogTitle("FILES ALONG WITH LINE NUMBERS");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            Map<String, Integer> result = new HashMap<String, Integer>();
            File directory = new File(chooser.getSelectedFile().getAbsolutePath());

            List<File> files = getFileListing(directory);

            //print out all file names, in the the order of File.compareTo()
            for (File file : files) {
                System.out.println("Directory: "+file);
                result = getFileLineCount(file);
                totalFileScannedCount +=  result.size();
            }


            System.out.println("*****************************************");
            System.out.println("FILE NAME FOLLOWED BY LOC");
            System.out.println("*****************************************");

            for (Map.Entry<String, Integer> entry : result.entrySet()) {
                System.out.println(entry.getKey() + " ==> " + entry.getValue());
            }
            System.out.println("*****************************************");
            System.out.println("SUM OF FILES SCANNED ==>" + "\t" + totalFileScannedCount);
            System.out.println("SUM OF ALL THE LINES ==>" + "\t" + totalLineCount);

        }

    }

    public static Map<String, Integer> getFileLineCount(File directory) throws FileNotFoundException {
        Map<String, Integer> result = new HashMap<String, Integer>();

        File[] files = directory.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File directory, String name) {
                if (name.endsWith(".java")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        for (File file : files) {
            if (file.isFile()) {
                Scanner scanner = new Scanner(new FileReader(file));
                int lineCount = 0;
                try {
                    for (lineCount = 0; scanner.nextLine() != null; lineCount++);
                } catch (NoSuchElementException e) {
                    result.put(file.getName(), lineCount);
                    totalLineCount += lineCount;
                }
            }
        }

        return result;
    }

    /**
     * Recursively walk a directory tree and return a List of all
     * Files found; the List is sorted using File.compareTo().
     *
     * @param aStartingDir is a valid directory, which can be read.
     */
    static public List<File> getFileListing(
            File aStartingDir) throws FileNotFoundException {
        validateDirectory(aStartingDir);
        List<File> result = getFileListingNoSort(aStartingDir);
        Collections.sort(result);
        return result;
    }

    // PRIVATE //
    static private List<File> getFileListingNoSort(
            File aStartingDir) throws FileNotFoundException {
        List<File> result = new ArrayList<File>();
        File[] filesAndDirs = aStartingDir.listFiles();
        List<File> filesDirs = Arrays.asList(filesAndDirs);
        for (File file : filesDirs) {
            if(file.isDirectory()) {
                result.add(file); 
            }
            if (!file.isFile()) {
                //must be a directory
                //recursive call!
                List<File> deeperList = getFileListingNoSort(file);
                result.addAll(deeperList);
            }
        }
        return result;
    }

    /**
     * Directory is valid if it exists, does not represent a file, and can be read.
     */
    static private void validateDirectory(
            File aDirectory) throws FileNotFoundException {
        if (aDirectory == null) {
            throw new IllegalArgumentException("Directory should not be null.");
        }
        if (!aDirectory.exists()) {
            throw new FileNotFoundException("Directory does not exist: " + aDirectory);
        }
        if (!aDirectory.isDirectory()) {
            throw new IllegalArgumentException("Is not a directory: " + aDirectory);
        }
        if (!aDirectory.canRead()) {
            throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
        }
    }
}

Now the problem is the issue with this approach is that if in java project named TESTRESULT there are 3 packages and each package having 2 files each then in the result on console it shows file name followed by loc of only the latest package only lets say in a java projest 3 packages named abc, def and tyu

com.abc package having files --->abc.java
com.def package having files --->abc.java
com.tyu package having files --->FileBrowse.java , FileCountLine.java

the outcome shown in console is... 


    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\.settings
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\bin
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\com
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\com\abc
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\com\def
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\tyu
    *****************************************
    FILE NAME FOLLOWED BY LOC
    *****************************************
    FileBrowse.java ==> 95
    FileCountLine.java ==> 53
    *****************************************
    SUM OF FILES SCANNED ==> 4
    SUM OF ALL THE LINES ==> 296

which is not perfect. please advise HOW IT WILL SHOW FILENAME FOLLOWED BY LOC for all the files

What I think is In the method that reads the files and the line count, you create a NEW Map for each directory and re-assign it to the same result. Since you do not display the data before you re-assign the Map, the previous Map is no longer accessible and by the time you get to the display you only have a Map for the last directory. Instead, you should maintain a single Map and insert new values from each directory into the same Map.

public static void main(String[] args) throws FileNotFoundException {  
            //...  
            for (File file : files) {  
                System.out.println("Directory: "+file);  
                result = getFileLineCount(file);

Right there you get a Map back from getFileLineCount(), and assign it to result. This discards the previously created result Map, and you lose everything that was already there. You have a few options:

  1. Pass the result Map into the getFileLineCount() method so you can add results to the one Map, rather than creating a new Map for each folder inside the getFileLineCount() method
  2. Take the results from getLineCount() and copy them into the result Map rather than replacing the result Map
  3. Create another collection, like a Map like: Map<String, Map<String, Integer>> (which would map the results returned by the getFileLineCount() method to the name of the directory those results pertain to), or List<Map<String, Integer>> (which would be a simple list of the results returned by getFileLineCount() without any mapping to the parent directory).

Please advise how to refactor my code ,Thanks in advance

Upvotes: 3

Views: 174

Answers (1)

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

You could do something like this:

result.put(file, lineCount);

instead of

result.put(file.getName(), lineCount);

And when you print the result you could print the pretty file name. Like this:

for (Map.Entry<File, Integer> entry : result.entrySet()) {
    System.out.println(entry.getKey().getName() + " ==> " + entry.getValue());
}

Of course map should be then declared as: Map.Entry<File, Integer>


Of course this can be improved a lot but with minimal changes you could have this (that is actually doing what you expect, please just change to print the name if you don't want the entire path):

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Scanner;

import javax.swing.JFileChooser;

public class abc {

    /**
     * @param args
     * @throws FileNotFoundException
     */
    private static int totalLineCount = 0;
    private static int totalFileScannedCount = 0;

    public static void main(final String[] args) throws FileNotFoundException {

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("C:" + File.separator));
        chooser.setDialogTitle("FILES ALONG WITH LINE NUMBERS");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            Map<File, Integer> result = new HashMap<File, Integer>();
            File directory = new File(chooser.getSelectedFile().getAbsolutePath());

            List<File> files = getFileListing(directory);

            // print out all file names, in the the order of File.compareTo()
            for (File file : files) {
                System.out.println("Directory: " + file);
                getFileLineCount(result, file);
            }

            System.out.println("*****************************************");
            System.out.println("FILE NAME FOLLOWED BY LOC");
            System.out.println("*****************************************");

            for (Map.Entry<File, Integer> entry : result.entrySet()) {
                System.out.println(entry.getKey().getAbsolutePath() + " ==> " + entry.getValue());
            }
            System.out.println("*****************************************");
            System.out.println("SUM OF FILES SCANNED ==>" + "\t" + totalFileScannedCount);
            System.out.println("SUM OF ALL THE LINES ==>" + "\t" + totalLineCount);
        }

    }

    public static void getFileLineCount(final Map<File, Integer> result, final File directory)
            throws FileNotFoundException {
        File[] files = directory.listFiles(new FilenameFilter() {

            public boolean accept(final File directory, final String name) {
                if (name.endsWith(".java")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        for (File file : files) {
            if (file.isFile()) {
                totalFileScannedCount++;
                Scanner scanner = new Scanner(new FileReader(file));
                int lineCount = 0;
                try {
                    for (lineCount = 0; scanner.nextLine() != null; lineCount++) {
                        ;
                    }
                } catch (NoSuchElementException e) {
                    result.put(file, lineCount);
                    totalLineCount += lineCount;
                }
            }
        }

    }

    /**
     * Recursively walk a directory tree and return a List of all Files found;
     * the List is sorted using File.compareTo().
     * 
     * @param aStartingDir
     *            is a valid directory, which can be read.
     */
    static public List<File> getFileListing(final File aStartingDir) throws FileNotFoundException {
        validateDirectory(aStartingDir);
        List<File> result = getFileListingNoSort(aStartingDir);
        Collections.sort(result);
        return result;
    }

    // PRIVATE //
    static private List<File> getFileListingNoSort(final File aStartingDir) throws FileNotFoundException {
        List<File> result = new ArrayList<File>();
        File[] filesAndDirs = aStartingDir.listFiles();
        List<File> filesDirs = Arrays.asList(filesAndDirs);
        for (File file : filesDirs) {
            if (file.isDirectory()) {
                result.add(file);
            }
            if (!file.isFile()) {
                // must be a directory
                // recursive call!
                List<File> deeperList = getFileListingNoSort(file);
                result.addAll(deeperList);
            }
        }
        return result;
    }

    /**
     * Directory is valid if it exists, does not represent a file, and can be
     * read.
     */
    static private void validateDirectory(final File aDirectory) throws FileNotFoundException {
        if (aDirectory == null) {
            throw new IllegalArgumentException("Directory should not be null.");
        }
        if (!aDirectory.exists()) {
            throw new FileNotFoundException("Directory does not exist: " + aDirectory);
        }
        if (!aDirectory.isDirectory()) {
            throw new IllegalArgumentException("Is not a directory: " + aDirectory);
        }
        if (!aDirectory.canRead()) {
            throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
        }
    }
}

Upvotes: 3

Related Questions