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");
}
}