/* * LineCounter.java: count non-empty lines in a file, where a line is empty * if it consists of just spaces. * * This illustrates using a file chooser dialog (with a frame) * and reading data from a file. * * Warning: the design used here is pretty simplistic; a real application * doing this sort of thing would likely have more classes. * * Author: Robert W. Hasker */ import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.FlowLayout; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class LineCounter extends JFrame { JLabel filenameLabel, countLabel; /** * Create the form. Warning: this is not terribly good design - * often the form creator is in a separate method. */ public LineCounter() { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(200, 150); setTitle("Line Counter"); filenameLabel = new JLabel("File: "); countLabel = new JLabel("Number of lines: "); setLayout(new FlowLayout()); add(filenameLabel); add(countLabel); } /** * Prompt user for file and return that file. * @return file to read */ public File fileToRead() { JFileChooser chooser = new JFileChooser("c:/"); chooser.setFileFilter(new FileNameExtensionFilter("Text Files", "txt")); int status = chooser.showOpenDialog(this); File targetFile = null; if (status == JFileChooser.APPROVE_OPTION) targetFile = chooser.getSelectedFile().getAbsoluteFile(); return targetFile; // will be null if user clicked on cancel or file doesn't exist } /** * Check that the file actually exists and return a scanner to * use to read it. * @param target file to be processed * @return scanner to process target file */ public Scanner fileStream(File target) { Scanner result = null; try { result = new Scanner(target); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "That file does not exist\n" + e.getMessage()); } return result; // will be null if user clicked on cancel or file doesn't exist } /** * Report number of lines in specified file. * @param filename name of file being processed * @param lines number of non-empty lines in specified file */ public void setResult(String filename, int lines) { filenameLabel.setText("File: " + filename); countLabel.setText("Number of non-empty lines: " + lines); } /** * Reads lines from input, counting ones which aren't just whitespace * @param source input stream to process * @return number of nonempty files */ public static int nonEmptyLinesInStream(Scanner source) { int count = 0; while (source.hasNextLine()) { String line = source.nextLine(); line = line.trim(); if (!line.isEmpty()) ++count; } return count; } /** * main driver: creates form, prompts user, prints results * @param args ignored command line arguments */ public static void main(String[] args) { LineCounter mainForm = new LineCounter(); mainForm.setVisible(true); File target = mainForm.fileToRead(); if ( target == null ) mainForm.setResult("[none]", 0); else { Scanner input = mainForm.fileStream(target); int lines = nonEmptyLinesInStream(input); mainForm.setResult(target.getName(), lines); } } }