1

When I run the following script to check inputQty greater than AvailQty I am getting the following:

java.lang.NumberFormatException: For input string: "97,045.1193"

This error occurs if availableQty is a decimal number. The value is delivered from a database, can you please correct where I am wrong?

   double AvailQty = Double.valueOf(AQTY.getValue());
   double inputQty = Double.valueOf(QTY.getValue());
   if(inputQty > AvailQty){
    session.setStatusMessage("Not Enough Quantity");
    //Abort operation
    throw new AbortHandlerException();
  }

Thanks

D V
  • 211
  • 6
  • 13
  • 2
    It can't format it because it doesn't accept the comma. You could do something like `Double.valueOf(INV.AVAILQTY.getValue().replace(",", ""))` and `Double.valueOf(XX_IGL_QTY.getValue().replace(",", ""))` to remove any commas before parsing – Matt May 21 '18 at 15:08
  • @Matt Do you want to convert it to an answer, or do you want to have the question closed as trivial typographical error? – Andrey Tyukin May 21 '18 at 15:09
  • 1
    The [docs](https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#valueOf(java.lang.String)) on Java `Double` actually provide you with a full specification of what's parseable from a `String`, as well as some complex regex to validate. TL;DR, the comma in there needs to be gone, e.g. by replacing it with an empty string. – Mena May 21 '18 at 15:10
  • @v-s you mentioned the value is delivered from a database - are you getting it from the database as a String, or are you getting it as a Double, formatting it as a String, then parsing it back to a Double? Because the best solution would probably be to go back and make your database field into a Double and not parse Strings at all – Matt May 21 '18 at 15:15

4 Answers4

1

It can't format it because it doesn't accept the comma. You could do something like Double.valueOf(INV.AVAILQTY.getValue().replaceAll(",", "")) and Double.valueOf(XX_IGL_QTY.getValue().replaceAll(",", "")) to remove any commas before parsing.

Matt
  • 3,303
  • 5
  • 31
  • 53
  • 1
    actually, String#replace will only replace the first occurence. So it will work with the given example, but will fail in a general case where the number is longer than 6 digits (and will contain more than 1 comma). Use #replaceAll instead – Jan Ossowski May 21 '18 at 15:11
  • 3
    @JanOssowski no. The difference between `String.replace` and `String.replaceAll` is that the first takes a literal (and invokes the second), while the second uses regex. You might be thinking about `replaceFirst`... – Mena May 21 '18 at 15:15
  • @Mena wow, you learn new things every day :) right you are sir – Jan Ossowski May 21 '18 at 15:20
  • 1
    @JanOssowski well those method names are definitely not human-friendly to be honest :D Reminds me a bit of `Matcher.matches` vs `Matcher.find`... – Mena May 21 '18 at 15:21
  • 1
    @Mena or BigDecimal#equals() failing because 0.0 is not 0.00 :D – Jan Ossowski May 21 '18 at 15:24
1

Your string contains commas, remove any commas before parsing.

Double.valueOf(INV.AVAILQTY.getValue().replaceAll(",", ""));
Eugen
  • 877
  • 6
  • 16
  • You do not need to use ```replaceAll(regex, String)``` here. A simple ```replace(String, String)``` will do just fine and you don't have to worry about regex – jseashell May 21 '18 at 15:20
0

Simple, your string contains commas. This is not legal. All you can have are numbers and a dot (decimal separator). I don't know where you get the value from, but if it's not something you can change on that side, you will have to do some hacking ;)

Double.valueOf(INV.AVAILQTY.getValue().replaceAll(",","");
Jan Ossowski
  • 328
  • 1
  • 2
  • 18
  • in the input qty field if I enter a value with , like 32,452.00 it should be displayed as 32452.00 the comma should not be visible what should I do in this case ? – D V May 21 '18 at 16:57
  • How can I know? How do you input your value? you would need to attach more code than just this usage – Jan Ossowski May 21 '18 at 20:58
0

Rather than re-formatting the string to do the conversion, you can tell the formatter to read the numbers using whatever format you like.

This answer shows how to change the formatting.

Here's a sample test to show how the same number in your question can be parsed successfully:

 @Test
public void showNumberFormats() throws ParseException {
    String rawAvailQty = "97,045.1193";
    String rawInptQty = "98,045.3421";

    NumberFormat nf = NumberFormat.getNumberInstance();

    double AvailQty = nf.parse(rawAvailQty).doubleValue();
    double inputQty = nf.parse(rawInptQty).doubleValue();
    if(inputQty > AvailQty){
     //Abort operation
     System.err.println("Could not perform operation");
   }
    System.out.println("Available qty: " + AvailQty);
    System.out.println("Input qty: " + inputQty);
}

Prints:

Could not perform operation
Available qty: 97045.1193
Input qty: 98045.3421
Cuga
  • 17,668
  • 31
  • 111
  • 166
  • 1
    In my opinion, this would be the best answer if you bothered to add an example inline instead of linking somewhere else. – Mena May 21 '18 at 15:20