How to Convert String to Int in Java

I think you will agree with me when I say:

Converting String to int in Java is a REALLY one of the most used operations.

I’ll show 10 methods of how to change String to int with descriptions and examples.

How to Convert a String to an Int in Java Methods Overview

  • If you need to parse String to primitive int – use Integer.parseInt
  • If you are sure that result is always positive and you should convert String to primitive int – use Integer.parseUnsignedInt
  • If you need to convert String to Integer object – use Integer.valueOf
  • If you have String in special format (“0x“, “0X“, “#“) – use Integer.decode
  • If you need to turn a string into an int and specify default value in case of NumberFormatException – use NumberUtils.toInt from apache.commons.lang

NOTE: It’s impossible to cast String object to primitive int or to Integer object. This code won’t compile. You should use methods described here to parse a string to int.

Integer.parseInt(s)

Converts a String value as a signed primitive int.

Method signature:

public static int parseInt(String s) throws NumberFormatException

Example:

int i = Integer.parseInt("100");
System.out.println(i);

Output:

100

Integer.parseInt(s, radix)

Parses a string argument as a signed primitive int in the radix specified by the second argument.

Method signature:

public static int parseInt(String s, int radix) throws NumberFormatException

Examples:

System.out.println(Integer.parseInt("0", 10));
System.out.println(Integer.parseInt("473", 10));
System.out.println(Integer.parseInt("+42", 10));
System.out.println(Integer.parseInt("-0", 10));
System.out.println(Integer.parseInt("-FF", 16));
System.out.println(Integer.parseInt("1100110", 2));
System.out.println(Integer.parseInt("2147483647", 10));
System.out.println(Integer.parseInt("-2147483648", 10));
System.out.println(Integer.parseInt("2147483648", 10));
System.out.println(Integer.parseInt("99", 8))
System.out.println(Integer.parseInt("Kona", 10));
System.out.println(Integer.parseInt("Kona", 27));

Output:

0
473
42
0
-255
102
2147483647
-2147483648
throws a NumberFormatException
throws a NumberFormatException
throws a NumberFormatException
411787

Since: Java 8

Integer.parseInt(s, beginIndex, endIndex, radix)

Turning a CharSequence value into a signed primitive int in the radix specified by the second argument from beginIndex to endIndex.

Note: The method has no protection against CharSequence mutation during parsing.

Method signature:

public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix) throws NumberFormatException

Examples:

String cssColor = "#4dba16";
int red   = Integer.parseInt(cssColor, 1, 3, 16);
int green = Integer.parseInt(cssColor, 3, 5, 16);
int blue  = Integer.parseInt(cssColor, 5, 7, 16);
System.out.println(red);
System.out.println(green);
System.out.println(blue);
        
int amount = Integer.parseInt("I have 2000$ on my bank account", 7, 11, 10);
System.out.println(amount);

Output:

77
186
22
2000

Since: Java 9

Integer.parseUnsignedInt(s)

Converts a String value to an unsigned primitive int.

Method signature:

public static int Integer.parseUnsignedInt​(String s) throws NumberFormatException

Examples:

int i = Integer.parseUnsignedInt("2017");
System.out.println(i);

Integer.parseUnsignedInt(s, radix)

Parses a String argument as an unsigned primitive int in the radix specified by the second argument.

Method signature:

public static int parseUnsignedInt(String s, int radix) throws NumberFormatException

Examples:

int iDecimal = Integer.parseUnsignedInt("75", 10); // base 10 decimal
System.out.println("iDecimal = " + iDecimal);
 
int iOctal = Integer.parseUnsignedInt("011", 8); // base 8 octal
System.out.println("iOctal = " + iOctal);
 
int iHexadecimal = Integer.parseUnsignedInt("aC", 16); // base 16 hexadecimal
System.out.println("iHexadecimal = " + iHexadecimal);

Since: Java 8

Integer.parseUnsignedInt(s, beginIndex, endIndex, radix)

Turning a CharSequence value into an unsigned primitive int in the radix specified by the second argument from beginIndex to endIndex.

Method signature:

public static int parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix) throws NumberFormatException

Examples:

int result1 = Integer.parseInt("010010000100001001000100", 0, 7, 2);
System.out.println(result1); 
 
int result2 = Integer.parseInt("-6B4A", 0, 3, 16); 
System.out.println(result2); 
 
int result2 = Integer.parseUnsignedInt("6B4A", 0, 3, 16);
System.out.println(result2);

Output:

36
-107
1716

Since: Java 9

Integer.valueOf(s)

Converts a String to an Integer object.

Method signature:

public static Integer.valueOf(String s) throws NumberFormatException

Example:

Integer i = Integer.valueOf("100");
System.out.println(i);

Output:

100

Integer.valueOf(s, radix)

Parses a String as an Integer object in the radix specified by the second argument.

Method signature:

public static Integer.valueOf(String s, int radix) throws NumberFormatException

Examples:

int binary = Integer.valueOf("11111111", 2);
System.out.println(binary);
 
int octal = Integer.valueOf("377", 8); 
System.out.println(octal);
 
int hexadecimal = Integer.valueOf("ff", 16); 
System.out.println(hexadecimal);

Output:

255
255
255

Since: Java 8

Integer.decode(s)

Decodes a String into an Integer. Accepts decimal, hexadecimal, and octal numbers in the following format:

SignOptNameExamples
±DecimalNumeral555, -475
±0XHexDigits0XA, -0X12A
±0xHexDigits0xA, -0x12A
±#HexDigits-#A, #123
±0OctalDigits010, -010

Method signature:

public static Integer decode(String nm) throws NumberFormatException

Examples:

System.out.println("octal numbers: " + Integer.decode("010")); 
System.out.println("hexadecimal numbers: " + Integer.decode("0XA")); 
System.out.println("minus hexadecimal numbers: " + Integer.decode("-0XA"));
System.out.println("minus hexadecimal numbers: " + Integer.decode("-#A"));

Output:

octal numbers: 8
hexadecimal numbers: 10
minus hexadecimal numbers: -10
minus hexadecimal numbers: -10

NumberUtils.toInt(s)

NumberUtils class contains some useful methods to parse strings. To Include it to the project you should add maven dependency:

<dependency>
   <groupId>commons-lang</groupId>
   <artifactId>commons-lang</artifactId>
   <version>2.6</version>
</dependency>

and import org.apache.commons.lang3.math.NumberUtils to your class.

Method signature:

public static int NumberUtils.toInts(String s)

Example:

int i = NumberUtils.toInt("100");
System.out.println(i);

Output:

100

Implementation with a default value as a second argument would be useful if you’re not sure what’s inside of the input String and you want to provide a fallback value.

Method signature:

public static int NumberUtils.toInts(String s, int defaultValue)

Example:

If the string is null, the default value is returned.

System.out.println(NumberUtils.toInt("100"));
System.out.println(NumberUtils.toInt(null, 1));
System.out.println(NumberUtils.toInt("", 2));
System.out.println(NumberUtils.toInt("qwerty", 3));

Output:

100
1
2
3

Leave a Comment