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
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ Integer.html
Integer (Java Platform SE 8 )
October 20, 2025 - + DecimalNumeral, HexDigits, and ... of The Javaโ„ข Language Specification, except that underscores are not accepted between digits. The sequence of characters following an optional sign and/or radix specifier ("0x", "0X", "#", or leading zero) is parsed as by the Integer.parseInt method with ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ number_parseint.htm
Java - parseInt() Method
Java Vs. C++ ... This method is used to get the primitive data type of a certain String. parseXxx() is a static method and can have one argument or two. ... parseInt(int i) โˆ’ This returns an integer, given a string representation of decimal, ...
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
[Java]Why will Integer.parseInt() not work for my String?
What IDE are you using? Did you install the official JDK? Can you check in your IDE what VM it's using? More on reddit.com
๐ŸŒ r/learnprogramming
13
1
December 4, 2015
what is meant by parseint?
In simple words, parseInt is a function used in java to convert string type data to integer type. More on youth4work.com
๐ŸŒ youth4work.com
21
1
May 8, 2020
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ integer-valueof-vs-integer-parseint-with-examples
Integer.valueOf() vs Integer.parseInt() with Examples - GeeksforGeeks
July 11, 2025 - It takes a valid string as a parameter and parses it into primitive data type int. It only accepts String as a parameter and on passing values of any other data type, it produces an error due to incompatible types.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 10 โ€บ java-integer-parseintmethod
Java Integer parseInt()Method
October 27, 2022 - Java Integer parseInt() method returns int value after parsing the given string using the specified radix.
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.

Find elsewhere
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ parseint-java
parseInt() Java Method: From Strings to Integers
February 26, 2024 - Like a skilled mathematician, Javaโ€™s parseInt method can transform a string of digits into a usable integer.
๐ŸŒ
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().
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ difference between parseint() and valueof() in java
Difference Between parseInt() and valueOf() in Java | Baeldung
January 8, 2024 - The class java.lang.Integer provides three variants of the parseInt() method. Letโ€™s look at each of them. The first variant of parseInt() accepts a String as a parameter and returns the primitive data type int. It throws NumberFormatException when it cannot convert the String to an integer.
๐ŸŒ
vteams
vteams.com โ€บ blog โ€บ use-of-parseint-java-programming
Learn the Basics & Ins-And-Outs of PARSEint Java Programming
February 23, 2024 - There are also some things you should keep in mind when using the PARSEInt while coding. Basically, PARSE int Java programming is a method that converts a string to an integer. So, when you are using the method you have to use two arguments.
๐ŸŒ
Global Tech Council
globaltechcouncil.org โ€บ home โ€บ what is parseint in java?
What is parseInt in Java? - Global Tech Council
July 5, 2025 - In Java, parseInt is a method used to convert a string into a whole number (an integer).
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java numbers โ€บ how to convert string to int in java
How to convert String to int in Java
parseInt is a static method of the Integer class that returns an integer object representing the specified String parameter. ... public class Demo2 { // convert string to int java public static void main(String args[]) { String str = "111"; ...
Published ย  December 23, 2024
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 614543 โ€บ java โ€บ Meaning-parseint-method
Meaning and Use of parseint method (Beginning Java forum at Coderanch)
Without the second parameter, the method simply takes your string and turns it into an int. see http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html ... Robert Lippens wrote:I recommend taking a look at Oracle's webdocs on the Integer class. When you use two parameters for the parseInt method, the first takes your string, tries to parse it, and the second is a radix - which is apparently the number of unique digits used to represent a number.
๐ŸŒ
Oracle
docs.oracle.com โ€บ en โ€บ java โ€บ javase โ€บ 20 โ€บ docs โ€บ api โ€บ java.base โ€บ java โ€บ lang โ€บ Integer.html
Integer (Java SE 20 & JDK 20)
July 10, 2023 - ... NumberFormatException - if the string does not contain a parsable unsigned integer. ... 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 ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ java-string-to-int-how-to-convert-a-string-to-an-integer
Java String to Int โ€“ How to Convert a String to an Integer
November 23, 2020 - In Java, we can use Integer.valueOf() and Integer.parseInt() to convert a string to an integer. This method returns the string as a primitive type int. If the string does not contain a valid integer then it will throw a NumberFormatException.
๐ŸŒ
YouTube
youtube.com โ€บ watch
parseInt Java Tutorial - String to Integer #56 - YouTube
$1,500 OFF ANY Springboard Tech Bootcamps with my code ALEXLEE1500. See if you qualify for the JOB GUARANTEE! ๐Ÿ‘‰ https://bit.ly/3HX970hThe parseIint java met...
Published ย  August 8, 2019