How to convert String to int in Java

In Java, we can use Integer.parseInt(String) to convert a String to an int; For unparsable String, it throws NumberFormatException.


  Integer.parseInt("1");      // ok
  Integer.parseInt("+1");     // ok, result = 1
  Integer.parseInt("-1");     // ok, result = -1
  Integer.parseInt("100");    // ok

  Integer.parseInt(" 1");     // NumberFormatException (contains space)
  Integer.parseInt("1 ");     // NumberFormatException (contains space)
  Integer.parseInt("2147483648"); // NumberFormatException (Integer max 2,147,483,647)
  Integer.parseInt("1.1");    // NumberFormatException (. or any symbol is not allowed)
  Integer.parseInt("1-1");    // NumberFormatException (- or any symbol is not allowed)
  Integer.parseInt("");       // NumberFormatException, empty
  Integer.parseInt(" ");      // NumberFormatException, (contains space)
  Integer.parseInt(null);     // NumberFormatException, null

Table of contents

1. Convert String to int

Below example uses Integer.parseInt(String) to convert a String "1" to a primitive type int.

ConvertStringToInt.java

package com.mkyong.string;

public class ConvertStringToInt {

  public static void main(String[] args) {

      String number = "1";

      // String to int
      int result = Integer.parseInt(number);

      // 1
      System.out.println(result);

  }
}

Output

Terminal

  1

2.How to handle NumberFormatException

2.1 For invalid number or unparsable String, the Integer.parseInt(String) throws NumberFormatException.


  String number = "1A";
  int result = Integer.parseInt(number); // throws NumberFormatException

Output

Terminal

Exception in thread "main" java.lang.NumberFormatException: For input string: "1A"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
	at java.base/java.lang.Integer.parseInt(Integer.java:668)
	at java.base/java.lang.Integer.parseInt(Integer.java:786)
	at com.mkyong.string.ConvertStringToInt.main(ConvertStringToInt.java:18)

2.2 The best practice is catch the NumberFormatException during the conversion.

ConvertStringToInt.java

package com.mkyong.string;

public class ConvertStringToInt {

  public static void main(String[] args) {

      String number = "1A";
      try {
          int result = Integer.parseInt(number);
          System.out.println(result);
      } catch (NumberFormatException e) {
          //do something for the exception.
          System.err.println("Invalid number format : " + number);
      }

  }

}

Output

Terminal

  Invalid number format : 1A

3. Integer.MAX_VALUE = 2147483647

Be careful, do not convert any numbers greater than 2,147,483,647, which is Integer.MAX_VALUE; Otherwise, it will throw NumberFormatException.

ConvertStringToInt2.java

package com.mkyong.string;

public class ConvertStringToInt2 {

  public static void main(String[] args) {

      // 2147483647
      System.out.println(Integer.MAX_VALUE);

      String number = "2147483648";
      try {
          int result = Integer.parseInt(number);
          System.out.println(result);
      } catch (NumberFormatException e) {
          //do something for the exception.
          System.err.println("Invalid number format : " + number);
      }

  }

}

Output

Terminal

2147483647
Invalid number format : 2147483648

4. Integer.parseInt(String) vs Integer.valueOf(String)

The Integer.parseInt(String) convert a String to primitive type int; The Integer.valueOf(String) convert a String to a Integer object. For unparsable String, both methods throw NumberFormatException.


  int result1 = Integer.parseInt("1");

  Integer result2 = Integer.valueOf("1");

5. Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-string

6. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
39 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Dillip
4 years ago

I am making a Movie Ticket System. User inputs seat number as “D13”. How to separate char row = ‘D’ and int column = 13?

Elle
4 years ago
Reply to  Dillip

You could use substring to separate your row from the column

anurag
6 years ago

Hello sir
this is well explained java String conversion.
really beautiful
thanks

Sumita Maity
4 years ago

if the string has trailing zeroes then the zeroes are getting truncated in java 8 , can you help ? I need to convert string 100100 to int 100100

khalid
4 years ago

tanks

Indra
4 years ago

Thanks for your information. it’s so helpfull

Ashish
4 years ago

Thanks for this explanation.

Makhdoom
5 years ago

what if I want 1 and 0 separately.

John
5 years ago

If my string has more than 10 digits,the compiler gives me an error,there’s a way to fix it? How can I read more than that from a string?

Robert Miielewczyk
5 years ago
Reply to  John

max 32 bit integer is 2,147,483,647 which is why it gives you an error you cant assign such a big number, you should assign it to 64 bit int, or leave it as a String

Chris Butterfield
4 years ago

Use type long

hasib
5 years ago

how can i assign it into 64 bit

Hasbi, Muh
5 years ago

Dear Sir MKYong,
Thank you for your simple but precise explanation.
Regards.

Devendra Gaud
5 years ago

thanks you

Jettyjie
6 years ago

why don’t use apache NumberUtils, or charge the String matchs d+,I think use Integer.valueof(String) directly is not good,we can avoid the exception