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(;

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

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


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



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



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

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

public String toString() {
return line;

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

JVM developer Java Scala