Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class words {
- private String fileContents;
- private String[] wordList;
- private ArrayList<String> longestWords;
- private Scanner S;
- private int words = 0, chars = 0, vowels = 0, lines = 0, bLines = 0, longest = 0;
- public words( File F ) throws FileNotFoundException {
- this.S = new Scanner( F );
- }
- public void changeFile( File F ) throws FileNotFoundException {
- this.S = new Scanner( F );
- }
- private void parseFile() {
- while( S.hasNext() ) {
- this.lines++;
- String temp = S.next();
- if( temp.equals( "\n" ) ) {
- this.bLines++;
- continue;
- }
- this.fileContents += temp;
- this.fileContents += "\n";
- }
- }
- private void getLongest() {
- parseFile();
- this.wordList = fileContents.split( "[ ~!@#$%^&*()_<>?,./:\\;\\[\\]\\\\`1234567890-=]" );
- /*
- for( int i = 0; i < this.wordList.length; ++i ) {
- wordList[i] = wordList[i].replace( null, " " );
- wordList[i] = wordList[i].trim();
- }
- */
- System.out.println( this.wordList[0] );
- this.words = this.wordList.length;
- this.longest = this.wordList[0].length();
- for( int i = 0; i < this.wordList.length; ++i ) {
- if( this.wordList[i].length() > this.longest ) {
- this.longestWords = new ArrayList<String>();
- this.longestWords.add( this.wordList[i] );
- this.longest = this.wordList[i].length();
- }
- if( this.wordList[i].length() == this.longest ) {
- System.out.println( this.wordList.length + ", " + i );
- this.longestWords.add( this.wordList[i] );
- }
- }
- //sanitizeWords();
- }
- private void sanitizeWords() {
- String letters = "qwertyuiopasdfghjklzxcvbnm";
- String vs = "euioa";
- for( int i = 0; i < this.wordList.length; ++i ) {
- for( int j = 0; j < this.wordList[i].length(); ++j ) {
- if( letters.contains( this.wordList[i].substring( j, j+1 ).toLowerCase() ) ) {
- this.chars++;
- if( vs.contains( this.wordList[i].substring( j, j+1 ).toLowerCase() ) )
- this.vowels++;
- }
- else {
- // encountered a non-letter character!
- // disassemble, fix, and reassemble the word in question
- char[] tWord = wordList[i].toCharArray();
- tWord[j] = ' ';
- String nWord = new String( tWord );
- nWord = nWord.trim();
- String[] nWords = nWord.split( " " );
- if( nWords.length > 1 ) {
- for( int k = 0; k < nWords.length; ++k ) {
- 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) ) ) {
- // the i'th object of nwords is itself a word
- this.words++;
- if( nWords[k].length() > this.longest ) {
- // the word we just found is the longest word, so save it and save the length
- this.longest = nWords[k].length();
- this.longestWords = new ArrayList<String>();
- this.longestWords.add( nWords[k] );
- }
- }
- }
- }
- }
- }
- }
- }
- public void run() {
- getLongest();
- 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 " );
- //String[] lWords = longestWords.toArray();
- for( int i = 0; i < this.longestWords.size(); ++i )
- System.out.print( this.longestWords.get(i) + ",\n" );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement