Java tutorial
public class Main { public static String compare(String st1, String st2) { if (st1.length() > st2.length()) { return st1; } else { return st2; } } public static void main(String[] args) { String str = "this is a test loooong test"; String stringArray[] = str.split("\\s"); String word = ""; for (int i = 0; i < stringArray.length; i++) { if (i == 0) { word = stringArray[0]; } word = compare(word, stringArray[i]); } System.out.println("Longest word = " + word); } }