Here you can find the source of getFloatingPoint(Console console, Function
static BigDecimal getFloatingPoint(Console console, Function<BigDecimal, Boolean> validator)
//package com.java2s; /*/* w w w.j a v a2 s.c o m*/ * Copyright (C) 2016 Yadieet SA <yadieet@gmail.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.io.Console; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.function.Function; public class Main { static BigDecimal getFloatingPoint(Console console, Function<BigDecimal, Boolean> validator) { if (validator == null) validator = value -> true; String answer; BigDecimal value; do { answer = readLine(console); try { value = stripAndOrRescale(new BigDecimal(answer)); if (validator.apply(value)) break; } catch (NumberFormatException ignored) { } } while (true); return value; } static BigDecimal getFloatingPoint(Console console, BigDecimal minValue, BigDecimal maxValue) { if (minValue.compareTo(maxValue) > 0) throw new IllegalArgumentException("Invalid min-max value."); return getFloatingPoint(console, value -> value.compareTo(minValue) >= 0 && value.compareTo(maxValue) <= 0); } private static String readLine(Console console) { console.printf("> "); return console.readLine().trim(); } private static BigDecimal stripAndOrRescale(BigDecimal value) { value = value.stripTrailingZeros(); try { value.toBigIntegerExact(); value = value.setScale(1, RoundingMode.UNNECESSARY); } catch (ArithmeticException ignored) { } return value; } }