read Line from Console - Java Language Basics

Java examples for Language Basics:Console

Description

read Line from Console

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(readLine());
    }//from   w  w  w .  j a v a2s  .com

    private static BufferedReader inputReader = new BufferedReader(
            new InputStreamReader(System.in));

    public static String readLine() {
        String line = "";
        try {
            line = inputReader.readLine();
            inputReader.mark(1);
            if (inputReader.ready()) { // This solves java's strange behavior of reading CRLF.
                if (inputReader.read() != '\n')
                    inputReader.reset();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return line;
    }
}

Related Tutorials