Saturday 14 February 2015

Java: Read String from console

Once again, I was out there trying to find the neatest solution for reading a string from the IDE console. It turns out that the shortest piece of code that one can write to achieve this would be :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

...

     public static String getString() throws IOException
     {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {;
            return br.readLine();
        }
    }