import java.io.File; import java.io.FileNotFoundException; import java.text.DecimalFormat; import java.util.Scanner; import uwcse.io.Input; public class FileInputExample { public static void main(String[] args) { // Create a Scanner to read the input file String fileName = new Input().readString("Input file name? "); System.out.println(); Scanner scan; try { scan = new Scanner(new File(fileName)); } catch (FileNotFoundException e) { System.out.println(fileName + " doesn't exist!"); return; } // Read the file and count the number of occurences of A, B, ... int[] count = new int[26]; while (scan.hasNextLine()) { String line = scan.nextLine(); line = line.toLowerCase(); for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (c >= 'a' && c <= 'z') { count[c - 'a']++; } } } scan.close(); int length = 0; for (int i = 0; i < count.length; i++) { length += count[i]; } // Display the statistics (as an histogram) DecimalFormat df = new DecimalFormat("0.00"); for (int i = 0; i < count.length && length > 0; i++) { double percent = count[i] * 100.0 / length; String display = "" + (char) ('a' + i); display += "(" + df.format(percent) + "%):\t"; for (int j = 1; j <= Math.round(percent); j++) { display += "X"; } System.out.println(display); } } }