Java BufferedReader console read string input

Introduction

To read a string from the keyboard, use the readLine() from BufferedReader class.

The following program demonstrates BufferedReader and the readLine() method.

The program reads and displays lines of text until you enter the word "stop":


// Read a string from console using a BufferedReader. 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
  public static void main(String args[]) throws IOException {
    // create a BufferedReader using System.in
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str;/*w  ww.  j av  a 2s  . com*/
    System.out.println("Enter lines of text.");
    System.out.println("Enter 'stop' to quit.");
    do {
      str = br.readLine();
      System.out.println(str);
    } while (!str.equals("stop"));
  }
}



PreviousNext

Related