a5 programm22
import javax.swing.JOptionPane; import java.io.*; import java.util.*; //This class shows how to use compute the frequencies of the symbols in a file. public class FrequencyTester { FrequencyList fList; public FrequencyTester() { fList = null; } public static void main(String args[]) { String fileName; Scanner file; String s; String inputLine; int i; FrequencyList fList; FrequencyTester ft = new FrequencyTester(); ft.fList = new FrequencyList(); fList = ft.fList; fileName = JOptionPane.showInputDialog("Enter filename."); System.out.println("Input File:"+fileName); System.out.println("**********Content of Input File**************"); try { file = new Scanner( new File( fileName ) ); while (file.hasNextLine()) { //read in line inputLine = file.nextLine(); System.out.println(inputLine); for ( i = 0;i < inputLine.length(); i++) { s = ""+inputLine.charAt(i); fList.insert(s); //insert symbol into linked list (increment count by 1 if already in list) } s = "\n"; //each line ends with a newline character (which is not captured in inputLine) fList.insert(s); //add it manually } System.out.println("**********End of Input File*************"); System.out.println("Symbols and their frequencies"); fList.printList(); //print the contents of the linked list of symbols and their frequencies. file.close(); }catch (IOException e) { System.out.println("IO Error: " + e.getMessage()); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } } }