Programs
package com.candidjava; import java.util.*; import java.io.*; public class TotalWords { public static void main(String[] args) throws FileNotFoundException { try { Scanner console = new Scanner(System.in); System.out.println("File to be read: "); String inputFile = console.next(); File file = new File(inputFile); Scanner a = new Scanner(file); int words = 0; while (a.hasNext()) { a.next(); words++; } System.out.println("Number of words: " + words); console.close(); a.close(); } catch (FileNotFoundException e) { System.out.print("No file found"); } } }
Output
File to be read:
D:\b.txt
Number of words: 6