Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8139723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:55:29+00:00 2026-06-06T11:55:29+00:00

I have developed a Java program that will count the number of files in

  • 0

I have developed a Java program that will count the number of files in the folder. There may be Java files or text files, of which it will count the number of lines of code. The idea is to print the file name to the console, followed by the line count. This part is done, shown below:

public class FileCountLine {    
    public static void main(String[] args) throws FileNotFoundException {
    Map<String, Integer> result = new HashMap<String, Integer>();
        File directory = new File("C:/Test/");
        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());
            }       
    }}

Right now I have hard coded the location of the directory like this:

File directory = new File("C:/Test/");

I want this to be more interactive, and prompt the user to enter the location into console. After the user presses Enter, it will do the rest of the functionality as is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:55:31+00:00Added an answer on June 6, 2026 at 11:55 am

    You can use System.out.println() to output a prompt to the console screen. Then, to get their path, use the Scanner class in java.util eg,

    Scanner lineIn = new Scanner(System.in);
    String input = lineIn.next();
    

    That string, input, can be your folder path but you probably want to do some error checking on the input for the correct format, good characters, etc

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a number of classes which manipulate files in Java. I am
I have developed a program that is counting the number of lines in a
I have developed a java program that runs fine in Mac OS X (10.6.7).
I have developed one project(java), In that i have copied few jar files and
I have a program developed under java with netbeans. It has a text pane
I have developed for a number of years in Java, primarily for Linux and
We have our own custom calendar in our portal which is developed in Java.
I have developed a Java Swing application, which uses the SwingWorker class to perform
I have developed the following program/architecture: A) A Java servlet receives POST requests, gets
I have an out-of-of-the-box software tool that I developed in Java. This software requires

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.