import java.io.*; import java.util.*; public class TextAnalysis22 { public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner (System.in); Scanner data = fileName(console); int count = 0; while(data.hasNext()) { String word = data.next(); count++; } System.out.println("The number of words in the given file is " + count); frequency(data.next(),console.next()); } public static Scanner fileName (Scanner console) throws FileNotFoundException{ System.out.println("Enter the path of the file to count the words"); File input = new File(console.nextLine()); while(!input.canRead()) { System.out.println("The given file is not found. Try again."); System.out.println("Enter the path of the file"); input = new File(console.nextLine()); } return new Scanner(input); } public static int frequency(String a,String s) { System.out.println("Enter the word to be Searched"); int counter = 0; String [] words = null; while(a!=null) { words = a.split(" "); for (String word : words) { if(word.equals(s)) { counter ++; System.out.println("The given word is present for "+counter+ " Times in the file"); } } } return counter; } }
frequency
HasNext
next
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)