chillurbrain

Task8_7_2

Dec 19th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class LongestWord {
  4.     Scanner sc = new Scanner(System.in);
  5.     String str = sc.nextLine();
  6.     String stringArray[] = str.split("\\s");
  7.     public String compare(String st1, String st2) {
  8.         if (st1.length() > st2.length()) {
  9.             return st1;
  10.         } else {
  11.             return st2;
  12.         }
  13.     }
  14.  
  15.     LongestWord() {
  16.         String word = "";
  17.         for (int i = 0; i < stringArray.length; i++) {
  18.             if (i == 0) {
  19.                 word = stringArray[0];
  20.             }
  21.             word = compare(word, stringArray[i]);
  22.         }
  23.         System.out.println("Longest word is " + word + "\nWord length is " + word.length());
  24.     }
  25.  
  26.     public static void main(String[] args) {
  27.         new LongestWord();
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment