Results 1 to 2 of 2
- 12-20-2010, 12:55 PM #1
Member
- Join Date
- Jan 2009
- Location
- Delft
- Posts
- 9
- Rep Power
- 0
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:
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.Java 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; }
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:
Java Code:valA = (new BigDecimal(sc.next().substring(2))).divide(new BigDecimal(1000));
- 12-20-2010, 01:25 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
If the String is always of the same format, why not simply split() on the space?
You can then deal with each bit of the split String as needed.
Scanner seems like overkill.
Similar Threads
-
connection fails
By jrjan in forum Advanced JavaReplies: 3Last Post: 01-10-2009, 12:15 AM -
Using Scanner to compute an average of the values
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:36 PM -
Repaint fails when using threads
By rjevans2000 in forum Threads and SynchronizationReplies: 1Last Post: 09-21-2007, 11:22 PM -
Repaint fails when using threads
By rjevans2000 in forum AWT / SwingReplies: 3Last Post: 08-15-2007, 05:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks