0

I am looking for method/way to accomplish this:

try {
    DecimalFormat decimalFormatter = new DecimalFormat();           
    decimalFormatter.setParseBigDecimal(true);
    decimalFormatter.parse(inflowTextField.getText());
}
catch(ParseException e)
{
    errorMsg+="Invalid inflow value! Must be BigInteger";
}

Above code is inside validation method for TextFields inside dialog window where user is editing some values and here I want to parse String from TextField.getText() into BigDecimal and if it is impossible then catch exception and make error string which is used in alert popup.

Thing is parse() method throws exception only when beginning of the string isn't number so examples like '1aaa1aaa1', '1111asdasdasd' etc. don't throw exception. I need them to do it.

In other words I need parser which will throw exception when string input isn't BigDecimal on the very beginning as whole.

ssukienn
  • 558
  • 6
  • 22
  • 2
    Possible duplicate of [Safe String to BigDecimal conversion](http://stackoverflow.com/questions/3752578/safe-string-to-bigdecimal-conversion) – DimaSan Oct 20 '16 at 17:16
  • 1
    Hint: it is always **dangerous** to assume that some exception has exactly one cause; and to completely drop the exception object itself ... like you are doing in your catch. Consider adding at least the message of that exception to your string. Or trace that somewhere. Instead of just throwing that information away. – GhostCat Oct 20 '16 at 17:22
  • You talk about `BigDecimal` but your string says `"Must be BigInteger"`. Which is it? – Rudy Velthuis Oct 20 '16 at 19:28
  • `BigDecimal`, msg is not updated. – ssukienn Oct 20 '16 at 19:36
  • In the end I just created `new BigDecimal(string)` which is ugly but anyway if string is fine GB will take care of it sooner or later (no reference to it, I think?) and if not it will cause `NumberFormatException` which is intented. And @GhostCat I know it is dangerous but my intention was only to make it working. – ssukienn Oct 21 '16 at 21:13

0 Answers0