Tuesday, February 1, 2011

How to read user input at command line -Java


    public String readUserInput(String printMessage) throws IOException{
        System.out.println(printMessage);
        InputStreamReader isr = new InputStreamReader ( System.in );
        BufferedReader br = new BufferedReader (isr);
        String returnString;
        try {
            returnString=br.readLine();
        } catch (IOException e) {
           String message="error while reading user input";
           log.error(message,e);
           throw new IOException(e);
        }
        finally {
            br.close();
            isr.close()
        }
        return returnString;
    }

No comments:

Post a Comment