Java Integer Create toInt(final String str, final int alt)

Here you can find the source of toInt(final String str, final int alt)

Description

A safe way to parse an integer elsewhere in the code.

License

Open Source License

Parameter

Parameter Description
str the input string, which should contain a parsable integer
alt the integer to return if the string does not contain a parsable integer

Declaration

public static int toInt(final String str, final int alt) 

Method Source Code

//package com.java2s;
/*/*from w w w.  ja  va  2s. c  om*/
 * Copyright (c) 2012 Jeremy N. Harton
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

public class Main {
    /**
     * A safe way to parse an integer elsewhere in the code. A default 
     * value is specified in the parameters, which will be returned if
     * a valid integer cannot be parsed from the input string. This
     * catches and handles the exception that may arise
     * 
     * @param str the input string, which should contain a parsable integer
     * @param alt the integer to return if the string does not contain a parsable integer
     * @return
     */
    public static int toInt(final String str, final int alt) {
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException nfe) {
            System.out.println("--- Stack Trace ---");
            nfe.printStackTrace();
            return alt;
        }
    }

    /**
     * Parse a String representation of a positive integer
     * and return an int
     * 
     * @param string integer represented as a string (digits 0-9)
     * @return
     */
    public static int parseInt(final String string) {
        int index = string.length() - 1;
        int n = 0;
        int temp = 0;

        int value = 0;

        boolean is_digit;

        while (index > -1) {
            is_digit = true;

            switch (string.charAt(index)) {
            case '0':
                temp = 0;
                break;
            case '1':
                temp = 1;
                break;
            case '2':
                temp = 2;
                break;
            case '3':
                temp = 3;
                break;
            case '4':
                temp = 4;
                break;
            case '5':
                temp = 5;
                break;
            case '6':
                temp = 6;
                break;
            case '7':
                temp = 7;
                break;
            case '8':
                temp = 8;
                break;
            case '9':
                temp = 9;
                break;
            default:
                is_digit = false;
                break;
            }

            if (is_digit) {
                value += temp * Math.pow(10, n);
            } else {
                throw new NumberFormatException();
            }

            index--;
            n++;
        }

        return value;
    }
}

Related

  1. toInt(final long a)
  2. toInt(final long n)
  3. toInt(final Object obj)
  4. toInt(final Object valueRep)
  5. toInt(final String s)
  6. toInt(final String str, int defaultValue)
  7. toInt(final String string)
  8. toInt(final String strPriority)
  9. toInt(final String text, final int defaultValue)