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
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-string-to-int-in-java
String to int in Java - GeeksforGeeks
The most common method to convert a string to a primitive int is Integer.parseInt(). It throws a NumberFormatException if the string contains non-numeric characters. ... // Java Program to demonstrate // String to int conversion using parseInt() ...
Published   July 23, 2025
🌐
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
How to convert a string to an integer in Java?
Hi all, I wanted to create this question/answer on how to convert a string to an integer in Java as this is something that I often get asked a lot about. More on digitalocean.com
🌐 digitalocean.com
1
September 24, 2021
what is meant by parseint?
parseint is a method to parea string to get number from it.. for example in java every thing we input during runtime is a string so to convert it to number i.e. More on youth4work.com
🌐 youth4work.com
21
1
May 7, 2020
How to convert a String to an int in Java language? - Studytonight
How can I convert a String to an int in Java? My String contains only numbers, and I want to return the number it repres More on studytonight.com
🌐 studytonight.com
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseInt
parseInt() - JavaScript | MDN
If it's unprovided, or if the value becomes 0, NaN or Infinity (undefined is coerced to NaN), JavaScript assumes the following: If the input string, with leading whitespace and possible +/- signs removed, begins with 0x or 0X (a zero, followed by lowercase or uppercase X), radix is assumed to be 16 and the rest of the string is parsed as a hexadecimal number. If the input string begins with any other value, the radix is 10 (decimal). Note: Other prefixes like 0b, which are valid in number literals, are treated as normal digits by parseInt().
🌐
Global Tech Council
globaltechcouncil.org › home › what is parseint in java?
What is parseInt in Java? - Global Tech Council
July 5, 2025 - It’s a method in the Integer class, which is part of the core Java library. 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
🌐
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
🌐
CodeAhoy
codeahoy.com › java › Convert-String-To-Int
Java String to int with examples | CodeAhoy
October 22, 2019 - Integer.parseInt(String s) is the most common way of converting numbers represented by String variables to int (primitive) in Java. public static int parseInt​(String s) throws NumberFormatException · The example below shows how to use the parseInt(...) method to convert a String variable, ...
🌐
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.
🌐
DigitalOcean
digitalocean.com › community › questions › how-to-convert-a-string-to-an-integer-in-java
How to convert a string to an integer in Java? | DigitalOcean
September 24, 2021 - Next, let’s learn how we could convert a string into an integer in Java using Integer.parseInt(). Now luckily for us, there is a method called Integer.parseInt(), which makes our life much easier. Let’s say, that we want to convert our data variable into an integer.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.integer.parseint
Integer.ParseInt Method (Java.Lang) | Microsoft Learn
Parses the string argument as a signed decimal integer. [Android.Runtime.Register("parseInt", "(Ljava/lang/String;)I", "")] public static int ParseInt(string s);
🌐
Tutorialspoint
tutorialspoint.com › java › lang › integer_parseint_radix.htm
Java - Integer parseInt() method
Following is the declaration for java.lang.Integer.parseInt() method · public static int parseInt(String s, int radix) throws NumberFormatException
🌐
Study.com
study.com › courses › 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 - In order to catch the exceptions, place the parseInt function within a try and catch block. The code you want to execute is inside the try statement. After the catch statement, you can display an error should the conversion fail. Since we're trying to convert a non-numeric number to numeric, the exception will be a NumberFormatException in the Java compiler. The code to wrap the pareseInt() function in a try and catch block is displayed here: String myString = newString("hello"); try { int makeNumber = Integer.parseInt(myString); System.out.println(makeNumber); } catch (NumberFormatexception e) { System.out.println("That wasn't a number!"); }
🌐
iO Flood
ioflood.com › blog › parseint-java
parseInt() Java Method: From Strings to Integers
February 26, 2024 - Many developers grapple with this task, but there’s a tool that can make this process a breeze. Like a skilled mathematician, Java’s parseInt method can transform a string of digits into a usable integer.
🌐
LabEx
labex.io › tutorials › java-how-to-parse-a-string-into-an-integer-value-in-java-417591
How to parse a string into an integer value in Java | LabEx
The most common way to parse a string into an integer in Java is by using the Integer.parseInt() method. This method takes a string as input and returns the corresponding integer value.
🌐
W3Schools
w3schools.com › jsref › jsref_parseint.asp
W3Schools.com
The parseInt method parses a value as a string and returns the first integer. A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10.
🌐
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().
🌐
Studytonight
studytonight.com › forum › how-to-convert-a-string-to-an-int-in-java-language
How to convert a String to an int in Java language? - Studytonight
How can I convert a String to an int in Java? My String contains only numbers, and I want to return the number it represents. For example, given the string "1234" the result should be the number 1234. ... String myString = "1234"; int foo = Integer.parseInt(myString); On the off chance that you take a gander at the Java documentation, you'll see the "get" is that this capacity can toss a NumberFormatException, which obviously you need to deal with