Java Parse Number parseNumber(CharacterIterator it)

Here you can find the source of parseNumber(CharacterIterator it)

Description

parse Number

License

Open Source License

Declaration

private static Object parseNumber(CharacterIterator it) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * /*from  ww w  . j a va2s  .c  om*/
 * Contributors: IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.math.BigDecimal;
import java.text.CharacterIterator;

public class Main {
    private static Object parseNumber(CharacterIterator it) {
        StringBuffer buffer = new StringBuffer();
        char c = it.current();
        while (Character.isDigit(c) || c == '-' || c == '+' || c == '.' || c == 'e' || c == 'E') {
            buffer.append(c);
            c = it.next();
        }
        try {
            return new BigDecimal(buffer.toString());
        } catch (NumberFormatException e) {
            throw error("expected a number but was '" + buffer.toString() + "'", it); //$NON-NLS-1$ //$NON-NLS-2$;
        }
    }

    private static RuntimeException error(String message, CharacterIterator it) {
        return new IllegalStateException("[" + it.getIndex() + "] " + message); //$NON-NLS-1$//$NON-NLS-2$
    }

    private static RuntimeException error(String message) {
        return new IllegalStateException(message);
    }
}

Related

  1. parseNum(String numStr)
  2. parseNumber(final String source, final String conversionPattern)
  3. parseNumber(String number)
  4. parseNumber(String number)
  5. parseNumber(String s)