Java Parse Int parseInteger(PushbackReader reader)

Here you can find the source of parseInteger(PushbackReader reader)

Description

parse Integer

License

Open Source License

Declaration

public static long parseInteger(PushbackReader reader) throws IOException, ParseException 

Method Source Code


//package com.java2s;
/*/*from   ww w .  j a v a  2s  .  co  m*/
 * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted under the terms of the
 * GNU General Public License.
 *
 * For redistributing this software or a derivative work under a license other
 * than the GPL-compatible Free Software License as defined by the Free
 * Software Foundation or approved by OSI, you must first obtain a commercial
 * license to this software product from Volker Bergmann.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

import java.io.PushbackReader;
import java.io.IOException;

import java.text.ParseException;
import java.text.ParsePosition;

public class Main {
    public static long parseInteger(PushbackReader reader) throws IOException, ParseException {
        boolean negative = parseOptionalSign(reader);
        return parseNonNegativeInteger(reader) * (negative ? -1 : 1);
    }

    public static boolean parseOptionalSign(PushbackReader reader) throws IOException {
        skipWhitespace(reader);
        int optionalSign = reader.read();
        if (optionalSign == '-')
            return true;
        if (optionalSign != -1)
            reader.unread(optionalSign);
        return false;
    }

    public static long parseNonNegativeInteger(String source, ParsePosition pos) throws ParseException {
        int digit;
        if (pos.getIndex() > source.length() || !Character.isDigit(digit = source.charAt(pos.getIndex())))
            throw new ParseException("Number expected", 0);
        pos.setIndex(pos.getIndex() + 1);
        long result = digit - '0';
        while (pos.getIndex() < source.length() && Character.isDigit(digit = source.charAt(pos.getIndex()))) {
            result = result * 10 + digit - '0';
            pos.setIndex(pos.getIndex() + 1);
        }
        return result;
    }

    public static long parseNonNegativeInteger(PushbackReader reader) throws IOException, ParseException {
        int digit;
        if ((digit = reader.read()) == -1 || !Character.isDigit((char) digit))
            throw new ParseException("Long expected", 0);
        long result = digit - '0';
        while ((digit = reader.read()) != -1 && Character.isDigit((char) digit))
            result = result * 10 + digit - '0';
        if (digit != -1)
            reader.unread(digit);
        return result;
    }

    public static void skipWhitespace(PushbackReader reader) throws IOException {
        int c;
        do {
            c = reader.read();
        } while (c != -1 && Character.isWhitespace((char) c));
        if (c != -1)
            reader.unread(c);
    }

    public static void skipWhiteSpace(String text, ParsePosition pos) {
        int i;
        while ((i = pos.getIndex()) < text.length() && Character.isWhitespace(text.charAt(i)))
            pos.setIndex(i + 1);
    }
}

Related

  1. parseInt(char[] input, ParsePosition offset, int length)
  2. parseInt(char[] strs, int beginindex, int endindex)
  3. parseInt(String src, Locale loc)
  4. parseInt(String stringValue)
  5. parseInt(String value)
  6. parseInteger(String text)