Guest User

Untitled

a guest
Jun 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package edu.upenn.cis.cis121.homework3;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7.  
  8. /**
  9.  * An implementation of the UNIX word count utility wc.
  10.  *
  11.  * @author Josh Margolis
  12.  * CIS 121, Spring 2012
  13.  *
  14.  */
  15. public class WordCount {
  16.  
  17.     /**
  18.      * Static method that processes the input.
  19.      * @param fileName - the name of the file to be opened
  20.      * @return String output formatted string specifying number of lines, words, and characters
  21.      * @throws IOException
  22.      **/   
  23.     public static String processFile(String fileName) throws IOException{
  24.         BufferedReader bReader = new BufferedReader(new FileReader(fileName));
  25.  
  26.         //holds if we are in a word or not, other variables are self-explanatory
  27.         boolean inWord = false;
  28.         int numChars = 0;
  29.         int numWords = 0;
  30.         int numLines = 0;
  31.        
  32.         //The current line we are in
  33.         String line = "";
  34.         while((line = bReader.readLine()) != null){
  35.             numLines++;//Add to line
  36.             numChars++;//Add to char because the line character will not be counted otherwise
  37.            
  38.             for (int i = 0; i<line.length(); i++) {
  39.                 numChars++;
  40.                 if(line.charAt(i) == '\t' || line.charAt(i) == ' '){
  41.                     //Check if were in a word
  42.                     if(inWord){
  43.                         numWords++;
  44.                         inWord = false;
  45.                     }
  46.                 }
  47.                 else
  48.                     inWord = true;
  49.             }
  50.            
  51.         }
  52.        
  53.         //Add one more if are still in a word
  54.         if(inWord == true)
  55.             numWords++;
  56.         bReader.close();//Clean up
  57.        
  58.         return (numLines + "\t" + numWords + "\t" + numChars + "\t" + fileName);
  59.     }
  60.     /**
  61.      * Program that takes a list of filenames as a command-line argument.
  62.      * For each file, program should output number of lines, words, and characters
  63.      * in the file. If no filename is provided, report that a filename argument is required
  64.      **/
  65.     public static void main(String args[]) {
  66.         ArrayList<String> rtnInfo = new ArrayList<String>();
  67.        
  68.         for(String arg : args)
  69.             try {
  70.                 //txtReader = new InputStreamReader(new FileInputStream (arg), "US-ASCII");
  71.                 //rtnInfo.agdd(processInput(txtReader) + "\t" + arg);
  72.                 rtnInfo.add(processFile(arg));
  73.             } catch (IOException e) {
  74.                 // Die, comment this out later
  75.                 System.out.println("File Error...");
  76.             }
  77.        
  78.         //Build the total numbers
  79.         int totals[] = {0,0,0};
  80.        
  81.         for(String x: rtnInfo){
  82.             System.out.println(x);
  83.             for(int i = 0; i < 3; i++)
  84.                 totals[i] += Integer.parseInt(x.split("\t")[i]);
  85.         }
  86.         //Print the totals
  87.         System.out.println(totals[0] + "\t" + totals[1] + "\t" + totals[2]);
  88.        
  89.     }
  90.  
  91. }
Add Comment
Please, Sign In to add comment