Advertisement
Guest User

Untitled

a guest
Sep 24th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. /* CS 007 Lab3.java
  2.     Reads a large file or words and gathers some simple stats
  3. */
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. public class Lab3
  8. {
  9.     public static void main (String args[]) throws Exception // i.e. the input file you put on cmd line is not in directory
  10.     {
  11.         // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE
  12.         if (args.length < 1 )
  13.         {
  14.             System.out.println("\nusage: C:\\> java Lab3 <input filename>\n\n"); // i.e. C:\> java Lab3 dictionary.txt
  15.             System.exit(0);
  16.         }
  17.         BufferedReader infile = new BufferedReader( new FileReader(args[0]) ); // we read our text file line by line .readLine()
  18.         int numOfWordsReadIn=0; // each line read in has exactly one word on it.
  19.         int numOfWordsWithAnyOf_aeiou=0; // contains any of these: a,e,i,o and u
  20.         int numOfWordsWith_y_ButNoneOf_aeiou=0;  // contains y but none of a,e,i,o or u
  21.         int numOfWordsWithNoVowels=0; // contains none of a, e, i, o, u, or y
  22.         int shortestWordLength=Integer.MAX_VALUE,longestWordLength=0;
  23.  
  24.         while( infile.ready() ) // BufferedReader's .ready() means same as Scanner's .hasNextLine()
  25.         {
  26.             String word = infile.readLine();
  27.             ++numOfWordsReadIn;
  28.             // ---------- DONT WRITE ANYTHING ABOVE THIS LINE -------------------
  29.             if (word.length()>longestWordLength)
  30.             {longestWordLength = word.length();
  31.        
  32.        
  33.            
  34.             }
  35.            
  36.             else if (word.length() < shortestWordLength)
  37.             {
  38.                 shortestWordLength = word.length();
  39.             }
  40.            
  41.            
  42.             for(int i = 0; i < word.length(); ++i)
  43.             {  
  44.                 char c = word.charAt(i);
  45.                 if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
  46.                     ++numOfWordsWithAnyOf_aeiou;
  47.                     break;
  48.                 }      
  49.             }
  50.            
  51.             for(int i = 0; i < word.length(); ++i){
  52.                 char c = word.charAt(i);
  53.                 if (c=='y') {
  54.                     for(int j = 0;j<word.length();j++){
  55.                         char d = word.charAt(j);
  56.                         if(d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u'){
  57.                             break;
  58.                         }
  59.                         else if(j==word.length()-1)
  60.                             {  ++numOfWordsWith_y_ButNoneOf_aeiou;
  61.                         }
  62.                        
  63.                     }
  64.                 }
  65.                
  66.             }
  67.            
  68.            
  69.            
  70.            
  71.            
  72.                
  73.            
  74.             // ----------- DONT WRITE ANYTHING BELOW THIS LINE -------------------
  75.  
  76.         } // END WHILE FILE HAS ANOTHER LINE IN IT
  77.         infile.close();
  78.        
  79.         // PRINT SUMMARY VARIABLES
  80.         System.out.println("total words read from file: " + numOfWordsReadIn );
  81.         System.out.println("longest  word length: " + longestWordLength );
  82.         System.out.println("shortest word length: " + shortestWordLength );
  83.         System.out.format("%6d words contained any of: a e i o u\n",numOfWordsWithAnyOf_aeiou );
  84.         System.out.format("%6d words contained 'y' but none of: a e i o u\n", numOfWordsWith_y_ButNoneOf_aeiou);
  85.         System.out.format("%6d words contained no vowels (i.e. none of: a e i o u y)\n", numOfWordsWithNoVowels );
  86.     } // END MAIN
  87. } // END LAB3 CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement