parseInt is the way to go. The Integer documentation says

Use parseInt(String) to convert a string to a int primitive, or use valueOf(String) to convert a string to an Integer object.

parseInt is likely to cover a lot of edge cases that you haven't considered with your own code. It has been well tested in production by a lot of users.

Answer from ggovan on Stack Overflow
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ number_parseint.htm
Java - parseInt() Method
parseInt(int i) โˆ’ This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input. public class Test { public static void main(String args[]) { int ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ Integer.html
Integer (Java Platform SE 8 )
April 21, 2026 - 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.
Discussions

Integer.parseInt is best way to convert String to Int in Java? - Stack Overflow
We normally use Integer.parseInt method for String to Integer conversion in JAVA, but when I check the implementation of parseInt method it goes way deep that I think initially. Below I m sharing how More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Good way to encapsulate Integer.parseInt() - Stack Overflow
If anything, I'm tempted to use Integer.parseInt and let autoboxing take care of it, to take advantage of the cache for small values. 2009-09-28T18:33:56.467Z+00:00 ... @Vlasec And not just Optional, but specialized versions for primitives, like OptionalInt. 2018-07-19T03:38:18.797Z+00:00 ... @sf_jeff: IMO, in idiomatic Java ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
do you know what Integer.getInteger(String) does in java?
Java is succinct. ... Yes sligowaths, it certainly does succ! ... I was being sarcastic. ... It has apparently failed to be as verbose as the OP would like. ... Maybe Gosling and/or Steele were so used to Lisp's GET that they didn't notice the inadequateness of the name and just copied it over. ... Of course you'd use Integer.parseInt... More on reddit.com
๐ŸŒ r/programming
310
302
February 6, 2011
How to convert a string = 1253478690 into integer in java?

The maximum value for the 32-bit int datatype and consequently also the Integer class, as documented by the field Integer.MAX_VALUE, is (2^31)-1 or 2147483647, which is less than 9876543210. So the exception is thrown because the number represented by the string you are supplying is too large, the number cannot be stored in an int.

In order to store larger values, you need to use the 64-bit long datatype and the Long class, then your new maximum limit will be Long.MAX_VALUE which is (2^63)-1 or 9223372036854775807.

More on reddit.com
๐ŸŒ r/javahelp
7
0
March 11, 2021
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ integer-valueof-vs-integer-parseint-with-examples
Integer.valueOf() vs Integer.parseInt() with Examples - GeeksforGeeks
May 27, 2026 - The Integer class provides two commonly used methods for this purpose: parseInt() and valueOf(). Integer.parseInt() returns a primitive int. Integer.valueOf() returns an Integer wrapper object.
๐ŸŒ
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.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ parseint() in java
parseInt() in Java - Scaler Topics
May 8, 2024 - It is used in Java for converting a string value to an integer by using the method parseInt().
๐ŸŒ
Global Tech Council
globaltechcouncil.org โ€บ home โ€บ blogs โ€บ what is parseint in java?
What is parseInt in Java? - Global Tech Council
July 5, 2025 - Hereโ€™s are some examples of parseInt in Java: The most common use of parseInt() is to convert a string that represents a decimal number into an integer.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Study.com
study.com โ€บ 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 - The string value must be an integer. It's important that the string value actually is numeric, or the code will fail. ParseInt() can accept a second parameter, called the radix, which will treat the first number as binary, octal, or hexadecimal.
๐ŸŒ
vteams
vteams.com โ€บ blog โ€บ use-of-parseint-java-programming
Learn the Basics & Ins-And-Outs of PARSEint Java Programming
February 23, 2024 - First you have to call the method from the integer calls to convert string to int Java programming, which you want to convert. Then the method will run and return you the desired results, provided everything went well. Here is a sample example. The above-mentioned, integer.parseInt Java example is passing โ€˜123โ€™ as a string and not a number into the method, which is being called in the second line from its integer class.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 7 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ Integer.html
Integer (Java Platform SE 7 )
Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed integer in the radix specified by the second argument, exactly as if the arguments were given to ...
๐ŸŒ
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.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ lang โ€บ integer_parseint.htm
Java - Integer parseInt() method
The Java Integer parseInt(String s) method parses the string argument s as a signed decimal integer. Following is the declaration for java.lang.Integer.parseInt() method s โˆ’ This is a String containing the int representation to be parsed.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 10 โ€บ java-integer-parseintmethod
Java Integer parseInt()Method
October 27, 2022 - public class JavaExample { public static void main(String[] args) { String s = "Hello1001Bye"; String s2 = "LEAF"; //taking 1001 part from string 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.println("int value: "+i2); } }
๐ŸŒ
Java Tutorial HQ
javatutorialhq.com โ€บ java tutorial โ€บ java.lang โ€บ integer โ€บ parseint() method example
Java Integer parseInt() method example
September 30, 2019 - The Integer.parseInt method is a very convenient Java API to transform a user input in String format into a more robust object type which in this case the Integer.Make a note that the parseInt method of Integer class is static thus it should be accessed statically.
๐ŸŒ
Quora
quora.com โ€บ How-does-Integer-parseInt-work-in-Java
How does Integer.parseInt() work in Java? - Quora
Answer (1 of 4): Usually this is done like this: * init result with 0 * for each character in string do thisresult = result * 10get 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 * retur...
Top answer
1 of 16
161

You could return an Integer instead of an int, returning null on parse failure.

It's a shame Java doesn't provide a way of doing this without there being an exception thrown internally though - you can hide the exception (by catching it and returning null), but it could still be a performance issue if you're parsing hundreds of thousands of bits of user-provided data.

EDIT: Code for such a method:

public static Integer tryParse(String text) {
  try {
    return Integer.parseInt(text);
  } catch (NumberFormatException e) {
    return null;
  }
}

Note that I'm not sure off the top of my head what this will do if text is null. You should consider that - if it represents a bug (i.e. your code may well pass an invalid value, but should never pass null) then throwing an exception is appropriate; if it doesn't represent a bug then you should probably just return null as you would for any other invalid value.

Originally this answer used the new Integer(String) constructor; it now uses Integer.parseInt and a boxing operation; in this way small values will end up being boxed to cached Integer objects, making it more efficient in those situations.

2 of 16
41

What behaviour do you expect when it's not a number?

If, for example, you often have a default value to use when the input is not a number, then a method such as this could be useful:

public static int parseWithDefault(String number, int defaultVal) {
  try {
    return Integer.parseInt(number);
  } catch (NumberFormatException e) {
    return defaultVal;
  }
}

Similar methods can be written for different default behaviour when the input can't be parsed.

๐ŸŒ
Quora
quora.com โ€บ What-do-you-mean-by-integer-parseInt-in-java
What do you mean by integer.parseInt in java? - Quora
Answer (1 of 8): The Integer.parseInt() java method is used primarily in parsing a String method argument into an Integer object. The Integer object is a wrapper class for the int primitive data type of java API. The Integer.parseInt method is a very convenient Java API to transform a user input...
๐ŸŒ
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 ...