Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileReader;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class FileAnalyzer
- {
- /**
- @param inputFileName the name of the input file
- */
- public static String longestWord(String inputFileName)
- throws FileNotFoundException
- {
- FileReader fr = new FileReader(inputFileName);
- String longestWord = "";
- String currentWord = "";
- try
- {
- while(true)
- {
- int ch = fr.read();
- if(ch == -1){break;}
- if(Character.toString(((char)ch)).matches("[, \n]"))
- {
- if(longestWord.length() < currentWord.length())
- {
- longestWord = currentWord;
- currentWord = "";
- }
- else
- {
- currentWord = "";
- }
- }
- else{currentWord += (char)ch;}
- }
- }
- catch(Exception e){}
- return longestWord;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement