/* Jeff Offutt - June 1989 (C version) */ /* stutter checks for repeat words in a text file.*/ /* Java version Spring 2008, with help from */ /* Steven Baker, SWE 437 */ import java.io.*; public class stutter { /** ******************************************** * Main method * Get the file name from the first argument or keyboard. * Call checkStutter. * If no file name is given, alert the user. * * @param args[] : the array of input arguments. ** ******************************************** **/ public static void main (String args[]) { stutter myStutter = new stutter(); if (args.length > 1) System.out.println ("Too many arguments.\nUsage: java stutter "); else if (args.length > 0) { try { BufferedReader reader = new BufferedReader (new FileReader (args[0])); checkStutter (reader); reader.close(); } catch (Exception e) { System.err.println ("Error reading from file " + args[0] + ": " + e.getMessage()); } } else // no argument, read from standard input { try { BufferedReader reader = new BufferedReader (new InputStreamReader(System.in)); checkStutter (reader); reader.close(); } catch (Exception e) { System.err.println ("Error reading from keyboard: " + e.getMessage()); } } } /** ******************************************** * Reads the input file one line, then one word at a time. * Looks for repeated words and informs users. * * @param input : the input stream ** ******************************************** **/ public static void checkStutter (BufferedReader input) throws IOException { int numberOfLines = 1; String line = null; String lastWord = null; line = input.readLine(); while (line != null) { // Remove all non alpha-numeric characters // This replaces the IsDelimit() function in the C version. line = line.replaceAll ("[^a-zA-Z0-9 ]", ""); // A small improvement over the C version - ignore case in the comparison line = line.toLowerCase(); // Splits the line into words and puts in an array of strings String[] words = line.split (" "); // Compare with the last word on the previous line if (words [0].equals (lastWord)) System.out.println ("Repeated word on line " + numberOfLines + ": " + words[0]); // Stop before the end, nothing to compare the last word with for (int i = 0; i < (words.length-1); i++) { // Check to see if the current and subsequent words are the same if (words [i].equals (words [i+1])) { System.out.println ("Repeated word on line " + numberOfLines + ": " + words [i]); } } // Save last word in the line lastWord = words [words.length-1]; line = input.readLine(); numberOfLines++; } } /** ******************************************** * Reads the input file one line, then one word at a time. * Looks for repeated words and informs users. * This alternate version of checkStutter has a different loop ... * * @param input : the input stream ** ******************************************** **/ public static void checkStutterB (BufferedReader input) throws IOException { String line = null; String lastWord = null; // Instead of a while loop with dispersed initialization and loop update, // put everything into this one rather complicated for loop. // Which one is better ? for (int numberOfLines = 1; (line = input.readLine()) != null; numberOfLines++) { // Remove all non alpha-numeric characters // This replaces the IsDelimit() function in the C version. line = line.replaceAll ("[^a-zA-Z0-9 ]", ""); // A small improvement over the C version - ignore case in the comparison line = line.toLowerCase(); // Splits the line into words and puts in an array of strings String[] words = line.split (" "); // Compare with the last word on the previous line if (words [0].equals (lastWord)) System.out.println ("Repeated word on line " + numberOfLines + ": " + words[0]); // Stop before the end, nothing to compare the last word with for (int i = 0; i < (words.length-1); i++) { // Check to see if the current and subsequent words are the same if (words [i].equals (words [i+1])) { System.out.println ("Repeated word on line " + numberOfLines + ": " + words [i]); } } // Save last word in the line lastWord = words [words.length-1]; } } }