Results 1 to 2 of 2
  1. #1
    Norberhuis is offline Member
    Join Date
    Jan 2009
    Rep Power

    Default Parser using Scanner fails on values <1


    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
    A 1.187 B 632.376 CA 1

    The code of the parser is:
    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 (!"A")) {
    			logger.trace("Did not find leading 'A' marker.");
    			throw new FlowclientDataParserException(data);
    		BigDecimal valA = null;
    		if (sc.hasNextBigDecimal()) {
    			valA = sc.nextBigDecimal();
    		} else {
    			logger.trace("Found no value parseable as BigDecimal for value A.");
    			throw new FlowclientDataParserException(data);
    		if (!"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:
    Java Code:
    valA = (new BigDecimal( BigDecimal(1000));

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power


    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

  1. connection fails
    By jrjan in forum Advanced Java
    Replies: 3
    Last Post: 01-10-2009, 01:15 AM
  2. Using Scanner to compute an average of the values
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:36 PM
  3. Repaint fails when using threads
    By rjevans2000 in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-21-2007, 11:22 PM
  4. Repaint fails when using threads
    By rjevans2000 in forum AWT / Swing
    Replies: 3
    Last Post: 08-15-2007, 05:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts