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 8462677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:13:59+00:00 2026-06-10T14:13:59+00:00

I have written the following very simple Java program to ask user enter a

  • 0

I have written the following very simple Java program to ask user enter a file name, then it will report the number of lines of this file to the standard output:

import java.io.*;
import java.util.*;

public class CountLine {

 public static void main(String[] args) 
 {

     //  prompt the user to enter their file name
    System.out.print("Please enter your file name: ");

    //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String fileName = null;

      //  read the username from the command-line; need to use try/catch with the
      //  readLine() method
      try {
         fileName = br.readLine();
      } catch (IOException ioe) {
         System.out.println("IO error trying to read your name!");
         System.exit(1);
      }

      System.out.println("Thanks for the file name, " + fileName);



    File file = new File("C:/Users/Will/Desktop/"+fileName);
    Scanner scanner;
    try {
        scanner = new Scanner(file);

    int count =0;
    String currentLine;

    while(scanner.hasNextLine())
    {
        currentLine=scanner.nextLine();
        count++;

    }

    System.out.println("The number of lines in this file is "+count);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        System.out.println("There is no such file");
        e.printStackTrace();
    }
}

}

It is working.I would be really thankful if experts could help me

  1. see if there is anything that can be improved in this code fragment,
  2. If the file is not found, the exception is caught in the outermost catch statement and print out the stack trace. However, I think it is not very user-friendly, is there a way if the file does not exist, then the whole process restarts from beginning?

Thanks in advance.

  • 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-10T14:14:01+00:00Added an answer on June 10, 2026 at 2:14 pm

    Get some Structure in your code:

    public static void main(String[] args) 
    {
      string output;
      string fname = readFileName();
      if (fileValid(fname)) //Ensure FileExists
      {
         int lineCount = scaneFile(fname);   
         output = "some output text including line numbers"   
      }  
      else
      {
        output = "File Not Valid..."
      }
      //showOutput...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a following code to get just the file name without extension
i have written the following code class Program { static void Main(string[] args) {
I have written the following code, CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); report.Load(@C:\Users\XXX\Desktop\Backup1\Project\ReportsFolder\ReportSalesInvoice.rpt); Report works
I have written a very simple socket server. It listens in post 63254. First
I am very new to C# and i have written the following class: using
I have the very simple following code: main.cpp #include ui_library_browser.h #include <QtGui/QApplication> #include StartWindow.h
I've written a very simple GUI in MATLAB that will convert temperatures. It is
I've written the following (very simple) control for use in an application (comments removed
I have a very simple TCP server written in C. It runs indefinitely, waiting
I am in the process of learning backbone.js and have written some very simple

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.