SHARE
TWEET

Untitled

advictoriam Mar 12th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top