-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTask6.java
More file actions
28 lines (17 loc) · 827 Bytes
/
Copy pathStringTask6.java
File metadata and controls
28 lines (17 loc) · 827 Bytes
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
import java.util.Scanner;
public class StringTask6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String enteredSentence = scanner.nextLine();
String[] arrayOfWords = enteredSentence.split(" ");
String longestWord = arrayOfWords[0];
for (int currentIndex = 1; currentIndex < arrayOfWords.length; currentIndex++) {
if (arrayOfWords[currentIndex].length() > longestWord.length()) {
longestWord = arrayOfWords[currentIndex];
}
}
System.out.println("The longest word is: " + longestWord);
System.out.println("Number of characters: " + longestWord.length());
}
}