1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
Answers
Answer:
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
\1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
1).Write a program to accept a sentence and display the longest word in the sentence? (Scanner class)
Explanation:
Answer:
Check out my solution!
package newPack;
import java.util.*;
public class longestWord {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String word = input.nextLine();
int length = 0;//we need to set this value = 0 to find the largest value
String [] s = word.split(" ");//divide the sentence into parts by space and store into String array
String longestWord = "";
for(int i = 0; i < s.length; i++) {
if(s[i].length() > length) {
length = s[i].length();
longestWord = s[i];
}
}
System.out.println(longestWord);
}
}
Explanation: