How to Subtract Datarecords
I scrape a site and I store 2 values. Low1 and Low2. I created a script using java that does the following "after each pattern application"
import java.lang.*;
int a,b,c;
a = Integer.parseInt(dataRecord.get( "low1" ));
b = Integer.parseInt(dataRecord.get( "low2" ));
c = a - b;
session.log(c);
this keeps giving me an error:
The error message was: The application script threw an exception: java.lang.NumberFormatException: For input string: "1,553" BSF info: null at line: 0 column: columnNo
I'm not a Java guy so any help would be appreciated.
The parseInt method may not
(edit: I left out the () after the "toString")
The parseInt method may not like the commas...
try this:
import java.lang.*;
int a,b,c;
a = Integer.parseInt(dataRecord.get( "low1" ).toString().replaceAll(",","");
b = Integer.parseInt(dataRecord.get( "low2" ).toString().replaceAll(",","");
c = a - b;
session.log(c);
this will remove the comma's from the String before it attempts to parse...
here's an even better
here's an even better option:
import java.lang.*;
int a,b,c;
a = Integer.parseInt(dataRecord.get( "low1" ).toString().replaceAll("[^\\d]+","");
b = Integer.parseInt(dataRecord.get( "low2" ).toString().replaceAll("[^\\d]+","");
c = a - b;
session.log(c);
that should remove any non-numerical characters from the string as the 'replaceAll' method uses a regex match...
I haven't tested it but I'm pretty sure the "\" needs to double up to escape it... the actual regex is: [^\d]+
if that doesn't work just remove one of the backslashes...
Ditto. So that you know,
Ditto.
So that you know, rc2009, the ultimate problem is that the
Integer.parseInt
method wants ONLY numbers, no "fancy" stuff like commas or spaces, etc.