import java.util.*;
/**
* Print n Longest Lines
* Programming challenge description:
* Write a program that finds the N longest lines from standard input.
* Input:
* Your program should read lines of text from standard input.
* The first line contains the value of N, which will be a positive integer.
* Ignore empty lines in the input. You can assume that your program will receive at least N non-empty lines.
* Output:
* Print to standard output the longest N lines from the input, sorted from longest line to shortest.
* Ensure that there are no trailing spaces on each line you print nor extra empty lines between your output lines.
*/
public class PrintLongestNLines {

public static void main(String[] args) {
System.out.println("======= starting application===");
List<InputLine> inputLines = new ArrayList<InputLine>();

Scanner scanner = new Scanner(System.in);

while (scanner.hasNext()) {
String nextLine = scanner.next().trim();

if(nextLine.length() > 0)
inputLines.add(new InputLine(nextLine));

}

int linesToPrint = Integer.parseInt(inputLines.get(0).toString());

scanner.close();

Collections.sort(inputLines);

List<InputLine> validLines = inputLines.subList(0, linesToPrint);
for (InputLine validLine: validLines) {
System.out.println(validLine);
}

}


}


class InputLine implements Comparable<InputLine> {
private String line;

public InputLine(String line){
this.line = line;
}

@Override
public String toString() {
return line;
}

@Override
public int compareTo(InputLine o) {
return this.line.length() - o.line.length();
}
}

JVM developer Java Scala