I have the application in which user select the particular folder and all the files in that folder along with the line of code in those individual files is counted and at the console along with the file names and the line of code in to them is displayed ..here is my piece of code …
Map<String, Integer> result = new HashMap<String, Integer>();
File directory = new File(chooser.getSelectedFile().getAbsolutePath());
File[] files = directory.listFiles();
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);
}
}
}
for (Map.Entry<String, Integer> entry : result.entrySet())
{ System.out.println(entry.getKey() + " ==> " + entry.getValue());
} }
but I want to add one more functionality it should also display total lines of all the files lets assume those 6 individual files have 10 piece of line of code each have then sum of lines is 10*6 that is 60 total lines read, how to achieve this please advise
Why not just add a counter for each Map entry value and add that to your counter/accumulator and print it out afterwards: