Write a program that prompts the user to enter a filename (use a Scanner to get the filename). The program then ouputs to the screen the number of lines, words, and characters in the file.
Here is my code: (I'm getting a infinite while loop):

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

public class question1
{
    public static void main (String [] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter input filename: ");
        String filename = keyboard.nextLine();

        int numberOfLines = 0;
        int numberOfWords = 0;
        char numberOfChar =0;

        try
        {
            PrintWriter fileOut = new PrintWriter (new File(filename));

                while (keyboard.hasNextLine())
                    {
                     numberOfChar = keyboard.next().charAt(0);

                    }

            fileOut.close();
        }

        catch (FileNotFoundException exception)
        {
            System.out.println("could not find your file, sorry it didn't work out");
        }

        System.out.println(numberOfChar + " characters");   
    }
}

(I'm getting a infinite while loop

Add some println calls in the loop that prints out the value of numberOfChar to see what the code is reading that makes it loop forever.

Have you read the API doc for the Scanner methods to see what they can do? For example they can block waiting for input.

You need to work out the logic for your program before writing code. The code that you posted doesn't do anything like what the assignment states.

Edited 4 Years Ago by NormR1

I don't know the logic , I need help understanding what to do, I know I have to have a while loop, but I have no idea what to put in it.

Before writing code, work on the logic for what the code should do. Don't try writing code until you know what you want the code to do.
Post the steps the code should do here and we'll try to get them in order before writing code.

I don't understand what your saying ?
It needs to read the file, then count the number of characters, lines and words, then output them to the screen. That is the logic?

Break that down into a list of the detailed steps the program needs to do. What you have written is a very high level description of what needs to be done. For example here are the starting steps:

Ask user for name of file to read
read name of file
open the file so it can be read
... fill in the rest of the steps
What are the steps to count the number of characters? the number of words, the number of lines?

Edited 4 Years Ago by NormR1

Now I need help with counting the words.

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

public class question1
{
    public static void main (String [] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter input filename: ");
        String filename = keyboard.nextLine();

        int counterChar = 0;
        int counterWords = 0;
        int counterLines = 0;

        Scanner fileOut = null;

        try
        {
            fileOut = new Scanner (new File(filename));

                while (fileOut.hasNextLine())
                    {
                    String line = fileOut.nextLine();
                    counterLines ++;

                    String [] words = line.split(" ");
                    counterWords = words.length;
                    counterWords ++;


                    for (int i= 0; i< line.length(); i++)
                        {
                        char c = line.charAt(i);
                        counterChar++;
                        }

                    }

            fileOut.close();
        }

        catch (FileNotFoundException exception)
        {
            System.out.println("could not find your file, sorry it didn't work out");
        }

        System.out.println(counterLines + " lines");
        System.out.println(counterWords+ " words");
        System.out.println(counterChar + " characters");    
    }

}
This article has been dead for over six months. Start a new discussion instead.