此版本是對自己的這篇博客中的源碼進(jìn)行了優(yōu)化:http://blog.csdn.net/haifengzhilian/article/details/7835404
package com.iflytek.util;
import java.util.*;
import java.util.regex.Pattern;/*** 類名:GetSafeInput* 作用:在控制臺中安全的讀入一個數(shù)值 ------ 整型或浮點(diǎn)型* * 方法:* 1,用 try ... catch ... (會損失效率,最好不要用)* 2,用正則表達(dá)式 * * 方法2--說明:* 阻塞的方式,用Scanner從控制臺安全的讀入一個數(shù)* * 創(chuàng)建時間:2012-8-6 15:20* * 修改時間:2012-8-6 16:31 * 說明: 添加:public static int getInt()* 添加:public static int getDouble()* 其他BUG修正* 2012-8-14 19:21* 說明: 添加 getString()方法;* 將類名由GetSafeNumber改為GetSafeInput* 2012-8-27 12:32* 說明: 對正則表達(dá)式進(jìn)行修改,去除負(fù)號的異常錯誤* * * @author 常維軍**/
public class GetSafeInput
{/*** 阻塞式的從控制臺讀入一個安全的整數(shù)* 說明:顯式使用Scanner對象* @param sc Scanner對象* @return 如果是整數(shù)則返回,如果不是整數(shù)繼續(xù)讀取,知道讀入一個整數(shù)結(jié)束*/public static int getInt(Scanner sc){String inputs = sc.next();while(!isInteger(inputs)) {System.out.println("格式不正確,請重新輸入");inputs = sc.next();}return Integer.valueOf(inputs);}/*** 阻塞式的從控制臺讀入一個安全的整數(shù)* 說明:隱式使用Scanner對象* @return 如果是整數(shù)則返回,如果不是整數(shù)繼續(xù)讀取,知道讀入一個整數(shù)結(jié)束*/public static int getInt(){Scanner sc = new Scanner(System.in);String inputs = sc.next();while(!isInteger(inputs)) {System.out.println("格式不正確,請重新輸入");inputs = sc.next();}return Integer.valueOf(inputs);}/*** 阻塞式的從控制臺讀入一個安全的浮點(diǎn)數(shù)* 說明:顯式使用Scanner對象* @param sc Scanner對象* @return 如果是浮點(diǎn)數(shù)則返回,如果不是浮點(diǎn)數(shù)繼續(xù)讀取,知道讀入一個正確的浮點(diǎn)數(shù)結(jié)束*/public static double getDouble(Scanner sc){String inputs = sc.next();while(!isDouble(inputs)) {System.out.println("格式不正確,請重新輸入");inputs = sc.next();}return Double.valueOf(inputs);}/*** 阻塞式的從控制臺讀入一個安全的浮點(diǎn)數(shù)* 說明:隱式使用Scanner對象* @return 如果是浮點(diǎn)數(shù)則返回,如果不是浮點(diǎn)數(shù)繼續(xù)讀取,知道讀入一個正確的浮點(diǎn)數(shù)結(jié)束*/public static double getDouble(){Scanner sc = new Scanner(System.in);String inputs = sc.next();while(!isDouble(inputs)) {System.out.println("格式不正確,請重新輸入");inputs = sc.next();}return Double.valueOf(inputs);}/*** 判斷是否是浮點(diǎn)數(shù)* 方法:利用正則表達(dá)式* @param s 需要判斷的字符串* @return 是浮點(diǎn)數(shù)返回真,否則返回假*/public static boolean isDouble(String s){Pattern pattern = Pattern.compile("(\\-?)[0-9]+(\\.?)[0-9]*");if(pattern.matcher(s).matches()) {return true;}return false;}/*** 判斷是否是整數(shù)* 方法:利用正則表達(dá)式* @param s 需要判斷的字符串* @return 是整數(shù)返回真,否則返回假*/public static boolean isInteger(String s){Pattern pattern = Pattern.compile("(\\-?)[0-9]+");if(pattern.matcher(s).matches()) {return true;}return false;}/*** 隱式使用Scanner對象,從控制臺讀入字符* 添加日期:2012-8-14* @return 從控制臺獲取的字符*/public static String getString() {String inputString = null;Scanner scanner = new Scanner(System.in);inputString = scanner.next();return inputString;}/*** 顯式使用Scanner對象,從控制臺讀入字符* 添加日期:2012-8-14* @return 從控制臺獲取的字符*/public static String getString(Scanner sc) {String inputString = null;inputString = sc.next();return inputString;}
}
轉(zhuǎn)載于:https://www.cnblogs.com/wjchang/archive/2012/08/15/3671577.html
總結(jié)
以上是生活随笔為你收集整理的java(安全方便的从控制台读入数据)[对Scanner类进行封装,用正则表达式判断]...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。