🌐
Tutorialspoint
tutorialspoint.com › home › java › java integer parsing
Java Integer Parsing
September 1, 2008 - public class Test { public static void main(String args[]) { int x =Integer.parseInt("9"); double c = Double.parseDouble("5"); int b = Integer.parseInt("444",16); System.out.println(x); System.out.println(c); System.out.println(b); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › integer-valueof-vs-integer-parseint-with-examples
Integer.valueOf() vs Integer.parseInt() with Examples - GeeksforGeeks
July 11, 2025 - Example: Java · // Java program to demonstrate working parseInt() public class GFG { public static void main(String args[]) { int decimalExample = Integer.parseInt("20"); int signedPositiveExample = Integer.parseInt("+20"); int signedNegativeExample = Integer.parseInt("-20"); int radixExample = Integer.parseInt("20", 16); int stringExample = Integer.parseInt("geeks", 29); System.out.println(decimalExample); System.out.println(signedPositiveExample); System.out.println(signedNegativeExample); System.out.println(radixExample); System.out.println(stringExample); } } Output: 20 20 -20 32 11670324 ·
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java integer parseint method
Java Integer parseInt Method
September 1, 2008 - Following is the declaration for java.lang.Integer.parseInt() method · public static int parseInt(String s) throws NumberFormatException · s − This is a String containing the int representation to be parsed.
🌐
Global Tech Council
globaltechcouncil.org › home › what is parseint in java?
What is parseInt in Java? - Global Tech Council
August 22, 2025 - String: This version of the method takes a single string parameter. The string should represent a valid integer value. For example, parseInt(“123”) will return the integer 123.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 7 )
The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
🌐
Javatpoint
javatpoint.com › java-integer-parseint-method
Java Integer parseInt() Method - Javatpoint
Java Integer parseInt() Method - The Java parseInt() method is a method of Integer class that belong to java.lang package. The parseInt() method in Java is crucial for converting string representations of numbers into actual integer values. This capability is fundamental in various programming ...
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-integer-parseintmethod
Java Integer parseInt()Method
Here, we have two char sequences ... s int i = Integer.parseInt(s, 5, 9, 10); //taking EA from the string s2 and using 16 to parse it as hex int i2 = Integer.parseInt(s2, 1, 3, 16); System.out.println("int value: "+i); //Hex "EA" is equal to 234 in decimal System.out.p...
🌐
LabEx
labex.io › tutorials › java-java-integer-parseint-method-117728
Java parseInt(String, int) | Integer Parsing Tutorial | LabEx
In the above code, we have created a class named ParseIntMethod that contains the main() method. The program prompts the user to enter a string value and a radix value. Then, it will parse the string value into a signed integer value relative ...
🌐
Scaler
scaler.com › home › topics › parseint() in java
parseInt() in Java - Scaler Topics
May 8, 2024 - It is used in Java for converting ... the parameter that specifies the number system to be used. For example, binary = 2, octal = 8, hexadecimal = 16, etc....
Find elsewhere
🌐
Study.com
study.com › courses › business courses › business 104: information systems and computer applications
How to Convert String to Int in Java - ParseInt Method - Lesson | Study.com
January 9, 2023 - String myString = newString("hello"); int makeNumber = Integer.parseInt(myString); System.out.println(makeNumber); In order to catch the exceptions, place the parseInt function within a try and catch block.
🌐
H2K Infosys
h2kinfosys.com › blog › introduction to parseint in java
Introduction to ParseInt in Java
September 24, 2024 - ParseInt, for instance, can be used to verify the validity of an email address before sending an email to it. A straightforward API that is simple to understand is used for parsing and validation.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.integer.parseint
Integer.ParseInt Method (Java.Lang) | Microsoft Learn
[<Android.Runtime.Register("parseInt", "(Ljava/lang/String;I)I", "")>] static member ParseInt : string * int -> int ... Parses the string argument as a signed integer in the radix specified by the second argument.
🌐
Java Tutorial HQ
javatutorialhq.com › java tutorial › java.lang › integer › parseint() method example
Java Integer parseInt() method example
September 30, 2019 - Specifying the radix input to Integer.parseInt java method would set the static method to use it as base number in parsing the input string argument. Like for example, if the string value is expected to be in hexadecimal format we would be invoking the method:
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › parseint in java
parseInt in Java: Everything You should Know | upGrad
June 25, 2025 - In the example above, the string "123abc" cannot be parsed as an integer because it contains non-digit characters. Therefore, NumberFormatException is thrown. The exception is caught in a try-catch block, and a corresponding error message is printed.
🌐
Codekru
codekru.com › home › integer.parseint() method in java with examples
Integer.parseInt() method in Java with Examples - Codekru
December 3, 2022 - public class Codekru { public static void main(String[] args) { String s1 = "A20"; System.out.println("int value representing s1 with radix 16: " + Integer.parseInt(s1,16)); } } ... This method was introduced in Java 9.
Top answer
1 of 8
47

Usually this is done like this:

  • init result with 0
  • for each character in string do this
    • result = result * 10
    • get the digit from the character ('0' is 48 ASCII (or 0x30), so just subtract that from the character ASCII code to get the digit)
    • add the digit to the result
  • return result

Edit: This works for any base if you replace 10 with the correct base and adjust the obtaining of the digit from the corresponding character (should work as is for bases lower than 10, but would need a little adjusting for higher bases - like hexadecimal - since letters are separated from numbers by 7 characters).

Edit 2: Char to digit value conversion: characters '0' to '9' have ASCII values 48 to 57 (0x30 to 0x39 in hexa), so in order to convert a character to its digit value a simple subtraction is needed. Usually it's done like this (where ord is the function that gives the ASCII code of the character):

Copydigit = ord(char) - ord('0')

For higher number bases the letters are used as 'digits' (A-F in hexa), but letters start from 65 (0x41 hexa) which means there's a gap that we have to account for:

Copydigit = ord(char) - ord('0')
if digit > 9 then digit -= 7

Example: 'B' is 66, so ord('B') - ord('0') = 18. Since 18 is larger than 9 we subtract 7 and the end result will be 11 - the value of the 'digit' B.

One more thing to note here - this works only for uppercase letters, so the number must be first converted to uppercase.

2 of 8
28

The source code of the Java API is freely available. Here's the parseInt() method. It's rather long because it has to handle a lot of exceptional and corner cases.

Copypublic static int parseInt(String s, int radix) throws NumberFormatException {
    if (s == null) {
        throw new NumberFormatException("null");
    }

    if (radix < Character.MIN_RADIX) {
        throw new NumberFormatException("radix " + radix +
            " less than Character.MIN_RADIX");
    }

    if (radix > Character.MAX_RADIX) {
        throw new NumberFormatException("radix " + radix +
            " greater than Character.MAX_RADIX");
    }

    int result = 0;
    boolean negative = false;
    int i = 0, max = s.length();
    int limit;
    int multmin;
    int digit;

    if (max > 0) {
        if (s.charAt(0) == '-') {
            negative = true;
            limit = Integer.MIN_VALUE;
            i++;
        } else {
            limit = -Integer.MAX_VALUE;
        }
        multmin = limit / radix;
        if (i < max) {
            digit = Character.digit(s.charAt(i++), radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            } else {
                result = -digit;
            }
        }
        while (i < max) {
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit(s.charAt(i++), radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            }
            if (result < multmin) {
                throw NumberFormatException.forInputString(s);
            }
            result *= radix;
            if (result < limit + digit) {
                throw NumberFormatException.forInputString(s);
            }
            result -= digit;
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
    if (negative) {
        if (i > 1) {
            return result;
        } else { /* Only got "-" */
            throw NumberFormatException.forInputString(s);
        }
    } else {
        return -result;
    }
}
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
October 20, 2025 - The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
🌐
iO Flood
ioflood.com › blog › parseint-java
parseInt() Java Method: From Strings to Integers
February 26, 2024 - Are you finding it challenging to convert strings to integers in Java? You’re not alone. Many developers grapple with this task, but there’s a tool that can make this process a breeze. Like a skilled mathematician, Java’s parseInt method can transform a string of digits into a usable integer.