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

The Archive Base Latest Questions

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

I am trying to write a program for school. The assignment is to loop

  • 0

I am trying to write a program for school. The assignment is to loop and ask the user to enter files. When either a file is not found or the user exits out the loop (in my case, they click cancel on a joptionpane) data for each of the files is output. So far this is my code:

/**
 * @param args
 */
public static void main(String[] args) {

    // Lists to hold file data
    List<String> fileNames = new ArrayList<String>();
    List<Integer> numChars = new ArrayList<Integer>();
    List<Integer> numWords = new ArrayList<Integer>();
    List<Integer> numLines = new ArrayList<Integer>();
    String inFile;

    while ( (inFile = JOptionPane.showInputDialog("Enter a file name:")) != null) {

        // Create new file object instance
        File file = new File(inFile);

        try {
            fileNames.add(inFile);  // File name as a string
            // Pass file objects
            numChars.add( getNumChars(file));
            numWords.add( getNumWords(file));
            numLines.add( getNumLines(file));
            System.out.println("entered try block");
        } catch(FileNotFoundException e) {
            System.out.println("Could not find file: " + inFile);
            // break out the loop and display data
            break;
        }
    }   // end while

    if (numChars.size() > 0)
        showFileData(fileNames, numChars, numWords, numLines);

    System.out.println("Program ended");

}

private static void showFileData(List<String> fileNames,
        List<Integer> numChars, List<Integer> numWords,
        List<Integer> numLines) {
    for (int i=0;i<numChars.size();i++) {
        System.out.println("Data for: " + fileNames.get(i));
        System.out.println("Number of characters: " + numChars.get(i));
        System.out.println("Number of words: " + numWords.get(i));
        System.out.println("Number of lines: " + numLines.get(i));
        System.out.println("-----------------------------------------------");
    }

}

private static int getNumLines(File file) throws FileNotFoundException {
    int c = 0;
    Scanner f = new Scanner(file);
    while(f.hasNextLine()) {
        c++;
    }
    return c;
}

private static int getNumWords(File file) throws FileNotFoundException {
    int c = 0;
    Scanner f = new Scanner(file);
    while (f.hasNextLine()) {
        String[] w = f.nextLine().split(" ");
        c += w.length;
    }
    return c;
}

private static int getNumChars(File file) throws FileNotFoundException {
    Scanner f = new Scanner(file);
    int c = 0;
    String line;
    while(f.hasNextLine()) {
        line = f.nextLine();
        String[] s = line.split(" ");
        for (int i=0;i<s.length;i++) {
            c += s[i].length();
        }
    }
    return c;
}

When I run the program, if I press cancel, it exits the loop and displays the stuff at the end like it’s supposed to. If I enter a file that doesn’t exist, it works how it’s supposed to. The only time it doesn’t work is when I enter a file that DOES exist, it doesn’t pop up another input dialog. In fact, it doesn’t seem to enter the try block. Have I set my program up correctly?

  • 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-11T11:55:07+00:00Added an answer on June 11, 2026 at 11:55 am

    You have an infinite loop in getNumLines(). You need a f.nextLine() in the while loop.

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

Sidebar

Related Questions

i am trying to write program,which ask user to enter some letters or strings
I am trying to write program for parsing and processing text file. After not
i am trying to write a program that prompts the user to enter a
I am trying to write a program that will prompt you to enter someone's
I am trying to write a program that sends basic text files from one
I trying to write a program that navigates the local file system using a
Im trying to write a program to read a text file through args but
I'm trying to write a program for school using the BigDecimal class. The program
I'm trying to write a program for school and don't know where to begin.
I am trying to write simple program using ONLY for loop ( if then

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.