-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestWord.java
33 lines (26 loc) · 955 Bytes
/
LongestWord.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.io.*;
public class LongestWord {
public String findLongestWordInLine(String line) {
String[] lineArray = line.split(" ");
Integer largestIndex = 0;
if (lineArray.length > 0) {
for (int i = 0; i < lineArray.length; ++i) {
if (lineArray[largestIndex].length() < lineArray[i].length())
largestIndex = i;
}
return lineArray[largestIndex];
}
return ""; // empty line or no words
}
public void processFile(BufferedReader in) throws IOException {
String line;
while ((line = in.readLine()) != null) {
System.out.println(findLongestWordInLine(line));
}
}
public static void main(String[] args) throws IOException {
File file = new File(args[0]);
BufferedReader in = new BufferedReader(new FileReader(file));
new LongestWord().processFile(in);
}
}