Java BufferedReader create from InputStreamReader and System.in to read console input

Description

Java BufferedReader create from InputStreamReader and System.in to read console input

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

public class Main {
  public static void main(String args[]) throws Exception {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);

    while (true) {
      System.out.print("Radius? ");
      String str = br.readLine();
      double radius;
      try {/*from  w  ww. jav  a2s.  c  o  m*/
        radius = Double.valueOf(str).doubleValue();
      } catch (NumberFormatException nfe) {
        System.out.println("Incorrect format!");
        continue;
      }
      if (radius <= 0) {
        System.out.println("Radius must be positive!");
        continue;
      }
      System.out.println("radius " + radius);
      return;
    }
  }
}



PreviousNext

Related