Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* CS 007 Lab3.java
- Reads a large file or words and gathers some simple stats
- */
- import java.io.*;
- import java.util.*;
- public class Lab3
- {
- public static void main (String args[]) throws Exception // i.e. the input file you put on cmd line is not in directory
- {
- // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE
- if (args.length < 1 )
- {
- System.out.println("\nusage: C:\\> java Lab3 <input filename>\n\n"); // i.e. C:\> java Lab3 dictionary.txt
- System.exit(0);
- }
- BufferedReader infile = new BufferedReader( new FileReader(args[0]) ); // we read our text file line by line .readLine()
- int numOfWordsReadIn=0; // each line read in has exactly one word on it.
- int numOfWordsWithAnyOf_aeiou=0; // contains any of these: a,e,i,o and u
- int numOfWordsWith_y_ButNoneOf_aeiou=0; // contains y but none of a,e,i,o or u
- int numOfWordsWithNoVowels=0; // contains none of a, e, i, o, u, or y
- int shortestWordLength=Integer.MAX_VALUE,longestWordLength=0;
- while( infile.ready() ) // BufferedReader's .ready() means same as Scanner's .hasNextLine()
- {
- String word = infile.readLine();
- ++numOfWordsReadIn;
- // ---------- DONT WRITE ANYTHING ABOVE THIS LINE -------------------
- if (word.length()>longestWordLength)
- {longestWordLength = word.length();
- }
- else if (word.length() < shortestWordLength)
- {
- shortestWordLength = word.length();
- }
- for(int i = 0; i < word.length(); ++i)
- {
- char c = word.charAt(i);
- if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
- ++numOfWordsWithAnyOf_aeiou;
- break;
- }
- }
- for(int i = 0; i < word.length(); ++i){
- char c = word.charAt(i);
- if (c=='y') {
- for(int j = 0;j<word.length();j++){
- char d = word.charAt(j);
- if(d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u'){
- break;
- }
- else if(j==word.length()-1)
- { ++numOfWordsWith_y_ButNoneOf_aeiou;
- }
- }
- }
- }
- // ----------- DONT WRITE ANYTHING BELOW THIS LINE -------------------
- } // END WHILE FILE HAS ANOTHER LINE IN IT
- infile.close();
- // PRINT SUMMARY VARIABLES
- System.out.println("total words read from file: " + numOfWordsReadIn );
- System.out.println("longest word length: " + longestWordLength );
- System.out.println("shortest word length: " + shortestWordLength );
- System.out.format("%6d words contained any of: a e i o u\n",numOfWordsWithAnyOf_aeiou );
- System.out.format("%6d words contained 'y' but none of: a e i o u\n", numOfWordsWith_y_ButNoneOf_aeiou);
- System.out.format("%6d words contained no vowels (i.e. none of: a e i o u y)\n", numOfWordsWithNoVowels );
- } // END MAIN
- } // END LAB3 CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement