Advertisement
advictoriam

Untitled

Mar 12th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.io.FileReader;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. public class FileAnalyzer
  6. {
  7.    /**
  8.       @param inputFileName the name of the input file
  9.    */
  10.    public static String longestWord(String inputFileName)
  11.       throws FileNotFoundException
  12.    {
  13.       FileReader fr = new FileReader(inputFileName);
  14.       String longestWord = "";
  15.       String currentWord = "";
  16.      
  17.       try
  18.       {
  19.          while(true)
  20.          {
  21.             int ch = fr.read();
  22.          
  23.             if(ch == -1){break;}
  24.             if(Character.toString(((char)ch)).matches("[, \n]"))
  25.             {
  26.                if(longestWord.length() < currentWord.length())
  27.                {
  28.                   longestWord = currentWord;
  29.                   currentWord = "";
  30.                }
  31.                else
  32.                {
  33.                   currentWord = "";
  34.                }
  35.             }
  36.             else{currentWord += (char)ch;}
  37.          }
  38.       }
  39.       catch(Exception e){}
  40.      
  41.       return longestWord;
  42.    }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement