🌐
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 ·
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseInt
parseInt() - JavaScript | MDN
If parseInt encounters a character ... and returns the integer value parsed up to that point. For example, parseInt("2", 2) returns NaN because 2 is not a valid numeral in the binary number system....
🌐
Global Tech Council
globaltechcouncil.org › home › what is parseint in java?
What is parseInt in Java? - Global Tech Council
July 5, 2025 - So, parseInt(“101”, 2) would return the integer 5, because 101 in binary is 5 in decimal. This method is a bit more advanced. It lets you convert a part of a string into an integer.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java integer parseint method
Java Integer parseInt Method
September 1, 2008 - public static int parseInt(String s) throws NumberFormatException · s − This is a String containing the int representation to be parsed. This method returns the integer value represented by the argument in decimal.
🌐
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 ...
🌐
iO Flood
ioflood.com › blog › parseint-java
parseInt() Java Method: From Strings to Integers
February 26, 2024 - The string is passed as an argument to the method, which then returns the integer value. The result is then printed to the console, outputting ‘123’. This is a basic way to use parseInt in Java, but there’s much more to learn about string to integer conversion.
🌐
W3Schools
w3schools.com › jsref › jsref_parseint.asp
JavaScript parseInt() Method
Only the first integer found is returned. Older browsers will return 8 for parseInt("010"). Older versions of ECMAScript used octal (radix 8) for values beginning with "0".
Find elsewhere
🌐
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.
🌐
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", "")] public static int ParseInt(string s); [<Android.Runtime.Register("parseInt", "(Ljava/lang/String;)I", "")>] static member ParseInt : string -> int ·
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-integer-parseintmethod
Java Integer parseInt()Method
In this example, the radix is not specified while calling parseInt() method so it parsed the strings using default radix 10 (10 is for decimal). public class JavaExample { public static void main(String[] args) { String s1 = "100"; String s2 = "+50"; String s3 = "-50"; int i1 = Integer.par...
🌐
Tpoint Tech
tpointtech.com › java-integer-parseint-method
Java Integer parseInt() Method - Tpoint Tech
March 17, 2025 - Integer parseInt() Java IdentityHashMap values() method · Java IdentityHashMap size() method · Java IdentityHashMap remove() method · Java IdentityHashMap putAll() method · Java IdentityHashMap put() method · Java IdentityHashMap keySet() method · Java IdentityHashMap isEmpty() method ·
🌐
LabEx
labex.io › tutorials › java-java-integer-parseint-method-117728
Java parseInt(String, int) | Integer Parsing Tutorial | LabEx
import java.util.Scanner; public ... Scanner(System.in); String s = sc.nextLine(); System.out.print("Enter the radix: "); int radix = sc.nextInt(); System.out.println("Entered value and Base value is: " + s + " and " + radix); ...
🌐
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:
🌐
Scaler
scaler.com › home › topics › parseint() in java
parseInt() in Java - Scaler Topics
May 8, 2024 - NullPointerException: It arises when the string s given as argument is null. ... A user-defined example where anyone using this code can put a value of their choice and get the equivalent integer as output.
🌐
H2K Infosys
h2kinfosys.com › blog › introduction to parseint in java
Introduction to ParseInt in Java
September 24, 2024 - The parseInt function of the Integer class receives the string “123” as an input in this example, and it transforms the string into the integer value 123 and stores it in the number variable.
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):

digit = 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:

digit = 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.

public 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;
    }
}
🌐
Codecademy
codecademy.com › docs › javascript › number methods › .parseint()
JavaScript | Number Methods | .parseInt() | Codecademy
May 31, 2024 - radix: An optional parameter that ... It is an integer between 2 and 36. If not provided, the radix defaults to 10, except when the string starts with 0x or 0X, which indicates a hexadecimal number. The following example demonstrates some of the use cases of the .parseInt() ...