Review of old java questions
7-2 Find the longest word-hebust (20 points)
Find the longest word (words of different length appear only once).
Input format: The
input format is a single line, separated by spaces between words.
Output format: The
output format is the longest word.
Input sample:
Here is a set of input. E.g:
an not need happy suggest
Output sample:
The corresponding output is given here. E.g:
suggest
years:
package Li;
import java.util.*;
public class Main{
public static void main(String[] args){
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
String s=in.nextLine();
String[] arr=s.split(" ");
int max=0;
int index=0;
for(int i=0;i<arr.length;i++) {
if(arr[i].length()>max) {
max=arr[i].length();
index=i;
}
}
System.out.println(arr[index]);
}
}