Parser using Scanner fails on values <1
Hello,
I am working on a parser needs to retrieve date of two sensors. The data is stored in a string of 29 characters and is given by a serial port.
The values can never go below 0.000 due to the hardware and business logicalwise cannot go above 1000.000.
Example of a string is:
A 0.999 B 661.844 CA 0
and
A 1.187 B 632.376 CA 1
The code of the parser is:
Code:
static FlowclientMeasurement doParsing(String data) {
logger.trace("Executing parsing on data: " + data);
final String dataErrorMsg = "data should be a 29-character string";
Validate.notEmpty(data, dataErrorMsg);
Validate.isTrue(data.length() == 29, dataErrorMsg);
Scanner sc = new Scanner(data);
if (!sc.next().equals("A")) {
logger.trace("Did not find leading 'A' marker.");
throw new FlowclientDataParserException(data);
}
BigDecimal valA = null;
if (sc.hasNextBigDecimal()) {
valA = sc.nextBigDecimal();
System.out.println(valA);
} else {
logger.trace("Found no value parseable as BigDecimal for value A.");
throw new FlowclientDataParserException(data);
}
if (!sc.next().equals("B")) {
logger.trace("Did not find 'B' marker.");
throw new FlowclientDataParserException(data);
}
BigDecimal valB = null;
if (sc.hasNextBigDecimal()) {
valB = sc.nextBigDecimal();
} else {
logger.trace("Found no value parseable as BigDecimal for value B.");
throw new FlowclientDataParserException(data);
}
final BigDecimal thousand = new BigDecimal(1000);
final long valueA = valA.multiply(thousand).longValue();
final long valueB = valB.multiply(thousand).longValue();
FlowclientMeasurement result = new FlowclientMeasurement(valueA, valueB);
logger.trace("Computed result: " + result);
return result;
}
Now this parser can parse the strings of data unless if one of the sensors are below 1. So it would not parse any value between 0.000 and 1.000.
Scanner does not recognise those values as a value. Are there any other scannerlike classes that might work for me? Thanks for your help!
Because i do not like this solution:
Code:
valA = (new BigDecimal(sc.next().substring(2))).divide(new BigDecimal(1000));