ConsoleReader.java

  1. package com.common.util;
  2. import java.io.BufferedReader;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.IOException;
  6. /**
  7.  * A class to read strings and numbers from an input stream. This class is
  8.  * suitable for beginning Java programmers. It constructs the necessary buffered
  9.  * reader, handles I/O exceptions, and converts strings to numbers.
  10.  */
  11. public class ConsoleReader
  12. {
  13.     /**
  14.      * Constructs a console reader from an input stream such as System.in
  15.      * 
  16.      * @param inStream
  17.      * an input stream
  18.      */
  19.     public ConsoleReader(InputStream inStream)
  20.     {
  21.         reader = new BufferedReader(new InputStreamReader(inStream));
  22.     }
  23.     /**
  24.      * Reads a line of input and converts it into an integer. The input line
  25.      * must contain nothing but an integer. Not even added white space is
  26.      * allowed.
  27.      * 
  28.      * @return the integer that the user typed
  29.      */
  30.     public int readInt()
  31.     {
  32.         String inputString = readLine();
  33.         int n = Integer.parseInt(inputString);
  34.         return n;
  35.     }
  36.     /**
  37.      * Reads a line of input and converts it into a floating- point number. The
  38.      * input line must contain nothing but a nunber. Not even added white space
  39.      * is allowed.
  40.      * 
  41.      * @return the number that the user typed
  42.      */
  43.     public double readDouble()
  44.     {
  45.         String inputString = readLine();
  46.         double x = Double.parseDouble(inputString);
  47.         return x;
  48.     }
  49.     /**
  50.      * Reads a line of input. In the (unlikely) event of an IOException, the
  51.      * program terminates.
  52.      * 
  53.      * @return the line of input that the user typed, null at the end of input
  54.      */
  55.     public String readLine()
  56.     {
  57.         String inputLine = "";
  58.         try
  59.         {
  60.             inputLine = reader.readLine();
  61.         } catch (IOException e)
  62.         {
  63.             System.out.println(e);
  64.             System.exit(1);
  65.         }
  66.         return inputLine;
  67.     }
  68.     private BufferedReader reader;
  69. }

ConsoleReader.java  Java 资料结构

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值