Your task is to open a file read all words in the file and f

Your task is to open a file, read all words in the file, and find the longest word in the file. If there are multiple words with the same length, return the first of the maximum length words.

Complete the following code:

Solution

import java.io.FileReader;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class FileAnalyzer

{

public static String longestWord(String inputFileName) throws FileNotFoundException

{

try

{

Scanner file= new Scanner(inputFileName);
String longestWord = \"\";
while (file.hasNextLine()) {
Scanner word = new Scanner(file.nextLine());
while (word.hasNext()) {
String str = word.next();
if(str.length() > longestWord.lenght())
longestWord = str;
}
}
System.out.println(\"Longest word is\"+longestWord);

}

catch(Exception e)

{

}

}

public static void main(String args[])

{

try{

File myFile = new File(\"file\"); //Please provide path to the file on which you want to test it.

longestWord(myFile);

}

catch(Exception e)

{

System.out.println(\"File not found\");

}

}

}

Your task is to open a file, read all words in the file, and find the longest word in the file. If there are multiple words with the same length, return the fir
Your task is to open a file, read all words in the file, and find the longest word in the file. If there are multiple words with the same length, return the fir

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site