Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class words {
  5.     private String fileContents;
  6.     private String[] wordList;
  7.     private ArrayList<String> longestWords;
  8.     private Scanner S;
  9.     private int words = 0, chars = 0, vowels = 0, lines = 0, bLines = 0, longest = 0;
  10.    
  11.     public words( File F ) throws FileNotFoundException {
  12.         this.S = new Scanner( F );
  13.     }
  14.    
  15.     public void changeFile( File F ) throws FileNotFoundException {
  16.         this.S = new Scanner( F );
  17.     }
  18.        
  19.     private void parseFile() { 
  20.         while( S.hasNext() ) {
  21.             this.lines++;
  22.             String temp = S.next();
  23.             if( temp.equals( "\n" ) ) {
  24.                 this.bLines++;
  25.                 continue;
  26.             }
  27.             this.fileContents += temp;
  28.             this.fileContents += "\n";
  29.         }
  30.     }
  31.    
  32.     private void getLongest() {
  33.         parseFile();
  34.        
  35.         this.wordList = fileContents.split( "[ ~!@#$%^&*()_<>?,./:\\;\\[\\]\\\\`1234567890-=]" );
  36.        
  37.         /*
  38.         for( int i = 0; i < this.wordList.length; ++i ) {
  39.             wordList[i] = wordList[i].replace( null, " " );
  40.             wordList[i] = wordList[i].trim();
  41.         }
  42.         */
  43.  
  44.         System.out.println( this.wordList[0] );
  45.        
  46.         this.words = this.wordList.length;
  47.        
  48.         this.longest = this.wordList[0].length();
  49.        
  50.         for( int i = 0; i < this.wordList.length; ++i ) {
  51.             if( this.wordList[i].length() > this.longest ) {
  52.                 this.longestWords = new ArrayList<String>();
  53.                 this.longestWords.add( this.wordList[i] );
  54.                 this.longest = this.wordList[i].length();
  55.             }
  56.             if( this.wordList[i].length() == this.longest ) {
  57.                 System.out.println( this.wordList.length + ", " + i );
  58.                 this.longestWords.add( this.wordList[i] );
  59.             }
  60.         }
  61.        
  62.         //sanitizeWords();
  63.     }
  64.        
  65.     private void sanitizeWords() {
  66.         String letters = "qwertyuiopasdfghjklzxcvbnm";
  67.         String vs = "euioa";
  68.         for( int i = 0; i < this.wordList.length; ++i ) {
  69.             for( int j = 0; j < this.wordList[i].length(); ++j ) {
  70.                 if( letters.contains( this.wordList[i].substring( j, j+1 ).toLowerCase() ) ) {
  71.                     this.chars++;
  72.                     if( vs.contains( this.wordList[i].substring( j, j+1 ).toLowerCase() ) )
  73.                         this.vowels++;
  74.                 }
  75.                 else {
  76.                     // encountered a non-letter character!
  77.                     // disassemble, fix, and reassemble the word in question
  78.                     char[] tWord = wordList[i].toCharArray();
  79.                     tWord[j] = ' ';
  80.                     String nWord = new String( tWord );
  81.                     nWord = nWord.trim();
  82.                     String[] nWords = nWord.split( " " );
  83.                     if( nWords.length > 1 ) {
  84.                         for( int k = 0; k < nWords.length; ++k ) {
  85.                             if( nWords[k].length() > 1 && letters.contains( nWords[k].substring(0, 1).toLowerCase() ) && letters.contains( nWords[k].substring(nWords[k].length(), nWords[k].length() + 1) ) ) {
  86.                                 // the i'th object of nwords is itself a word
  87.                                 this.words++;
  88.                                 if( nWords[k].length() > this.longest ) {
  89.                                     // the word we just found is the longest word, so save it and save the length
  90.                                     this.longest = nWords[k].length();
  91.                                     this.longestWords = new ArrayList<String>();
  92.                                     this.longestWords.add( nWords[k] );
  93.                                 }
  94.                             }
  95.                         }
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.     }
  101.    
  102.     public void run() {
  103.         getLongest();
  104.         System.out.print( "There are " + this.words + " words in the file,\n" + this.lines + " lines in the file, of which " + this.bLines + " are blank.\nthere are " + this.vowels + " vowels in the file, out of\n" + this.chars + " letters total\nthe longest word(s) in the file are " + this.longest + " letters long, and is/are " );
  105.         //String[] lWords = longestWords.toArray();
  106.         for( int i = 0; i < this.longestWords.size(); ++i )
  107.             System.out.print( this.longestWords.get(i) + ",\n" );
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement