parseInt() in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The method parseInt() belongs to the Integer class which is under java.lang package. It is used to parse the string value as a signed decimal value. It is used in Java for converting a string value to an integer by using the method parseInt().

Syntax of parseInt() in Java

Following is how the parseInt() method can be declared in java:

Parameters of parseInt() in Java

Data typeParameterDescription
intstartIndexIt is the starting index of the string and is inclusive.
intendIndexIt is the ending index of the string and is exclusive.
intradixIt represents the base of the number in the string.
StringsThe string that is to be converted into an integer.
CharSequencesIt is the sequence of characters that is to be converted to an integer.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return Values of parseInt() in Java

Method nameReturn
parseInt(String s)Returns an integer value in its decimal equivalent
parseInt(String s, int radix)Returns an integer value with base specified as radix.

Note :
Radix is the parameter that specifies the number system to be used.

For example, binary = 2, octal = 8, hexadecimal = 16, etc.

Exceptions of parseInt() in Java

There are three types of exceptions possible by using the method parseInt() in java.

NullPointerException: It arises when the string s given as argument is null.

IndexOutOfBoundsException: It arises when:

  • startIndex is negative
  • startIndex > endIndex
  • endIndex > s.length (length of given string)

NumberFormatException: It occurs when:

  • CharSequence does not contain parsable int at a specified radix
  • radix << Character.MIN_RADIX
  • radix >> Character.MAX_RADIX

Note:

Example of parseInt() in Java

Output:

More Examples

Example 1

Two strings are passed and are returned as their respective signed decimal Integer objects

Output:

Example 2

A user-defined example where anyone using this code can put a value of their choice and get the equivalent integer as output.

Output:

Explanation:

  • Here, trim() is used to removed ending spaces from the left and right sides.
  • These spaces otherwise, can cause NumberFormatException in Java.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Conclusion

  • The method parseInt() belongs to the Integer class which is under java.lang package. It is used to parse the string value as a signed decimal value.
  • Following are how the parseInt() method can be declared in java :
    • public static int parseInt (String s).
    • public static int parseInt (String s, int radix).
    • public static int parseInt (CharSequence s, int startIndex, int endIndex, int radix).
  • It is used in java for converting a string value to an integer by using the method parseInt().
  • The parseInt() method throws three exceptions: NullPointerException, IndexOutOfBoundsException, NumberFormatException when the input arguments are not according to the conventions.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more