|
| 1 | +package ch_16.exercise16_11; |
| 2 | + |
| 3 | +import javafx.scene.Scene; |
| 4 | +import javafx.scene.control.Button; |
| 5 | +import javafx.scene.control.Label; |
| 6 | +import javafx.scene.control.TextField; |
| 7 | +import javafx.scene.layout.VBox; |
| 8 | +import javafx.stage.Stage; |
| 9 | + |
| 10 | +import java.io.File; |
| 11 | + |
| 12 | +/** |
| 13 | + * **16.11 (Create a histogram for occurrences of letters) Write a program that reads a |
| 14 | + * file and displays a histogram to show the occurrences of each letter in the file, |
| 15 | + * as shown in Figure 16.40b. The file name is entered from a text field. Pressing |
| 16 | + * the Enter key on the text field causes the program to start to read and process |
| 17 | + * the file and displays the histogram. The histogram is displayed in the center of the |
| 18 | + * window. Define a class named Histogram that extends Pane. The class contains the property counts that is an array |
| 19 | + * of 26 elements. counts[0] stores the |
| 20 | + * number of A, counts[1] the number of B, and so on. The class also contains a |
| 21 | + * setter method for setting a new counts and displaying the histogram for the new |
| 22 | + * counts. |
| 23 | + * <p> |
| 24 | + * Example Filename input: |
| 25 | + * C:\Users\Harry\IdeaProjects\intro-to-java-programming\ch_16\exercise16_11\testFile.txt |
| 26 | + * (Right-click on the testFile.txt and choose 'copy absolute path') |
| 27 | + */ |
| 28 | +public class Exercise16_11 extends javafx.application.Application { |
| 29 | + private int[] counts = new int[26]; |
| 30 | + |
| 31 | + @Override |
| 32 | + public void start(Stage primaryStage) throws Exception { |
| 33 | + Histogram histogram = new Histogram(new int[26]); |
| 34 | + Label label = new Label("Filename:"); |
| 35 | + TextField fileInputField = new TextField(); |
| 36 | + Button viewHistogramButton = new Button("View"); |
| 37 | + VBox parentBox = new VBox(); |
| 38 | + double WIDTH = 800; |
| 39 | + histogram.setPrefWidth(WIDTH); |
| 40 | + double HEIGHT = 500; |
| 41 | + histogram.setPrefHeight(HEIGHT); |
| 42 | + parentBox.setPrefWidth(WIDTH); |
| 43 | + parentBox.setPrefHeight(HEIGHT); |
| 44 | + VBox.setVgrow(histogram, javafx.scene.layout.Priority.ALWAYS); |
| 45 | + parentBox.getChildren().add(histogram); |
| 46 | + parentBox.getChildren().addAll(label, fileInputField, viewHistogramButton); |
| 47 | + viewHistogramButton.setOnMousePressed(event -> { |
| 48 | + String filePath = fileInputField.getText(); |
| 49 | + if (filePath.isEmpty()) { |
| 50 | + return; |
| 51 | + } |
| 52 | + File file = new File(filePath); |
| 53 | + if (!file.exists()) { |
| 54 | + System.out.println("File does not exist"); |
| 55 | + return; |
| 56 | + } |
| 57 | + histogram.setCounts(countLetterFromFile(file, counts)); |
| 58 | + }); |
| 59 | + Scene scene = new Scene(parentBox, WIDTH, HEIGHT); |
| 60 | + primaryStage.setResizable(false); |
| 61 | + primaryStage.setScene(scene); |
| 62 | + primaryStage.show(); |
| 63 | + } |
| 64 | + |
| 65 | + public static int[] countLetterFromFile(File file, int[] counts) { |
| 66 | + String s = ""; |
| 67 | + try (java.util.Scanner input = new java.util.Scanner(file)) { |
| 68 | + while (input.hasNext()) { |
| 69 | + s = input.nextLine(); |
| 70 | + for (int i = 0; i < s.length(); i++) { |
| 71 | + char c = s.charAt(i); |
| 72 | + if (Character.isLetter(c)) { |
| 73 | + int index = Character.toUpperCase(c) - 'A'; |
| 74 | + counts[index]++; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } catch (Exception e) { |
| 79 | + System.out.println("Error reading file"); |
| 80 | + System.out.println("Error Message: " + e.getMessage()); |
| 81 | + } |
| 82 | + |
| 83 | + return counts; |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +} |
0 commit comments