×

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified by the second argument. The third parameter parses the CharSequence argument as a signed int in the specified radix, beginning at the specified beginIndex and extending to endIndex -1. Syntax
  1. public static int parseInt(String s) throws NumberFormatException
  2. public static int parseInt(String s, int radix) throws NumberFormatException
  3. public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix) throws NumberFormatException
Parameters s- a String containing the int representation to be parsed. radix- the radix to be used while parsing s. beginIndex- the beginning index. endIndex- the ending index.  Return Value This method returns an integer value represented by the argument in decimal or by the subsequence in the specified radix. Throws This method throws: NumberFormatException- if the String does not contain a parsable int. IndexOutOfBoundsException- if beginIndex is negative, or if beginIndex is greater than endIndex or if endIndex is greater than s.length(). NumberFormatException- if the CharSequence does not contain a parsable int in the specified radix, or if radix is either smaller than Character.MAX_RADIX or larger than Character.MAX_RADIX.  Example 1
public class JavaIntegerParseIntExample1 {
    public static void main(String[] args) {
        String s="123";
        //parses the CharSequence argument as a signed decimal integer
        int val=Integer.parseInt(s);
        System.out.println(val);
    }
}
Output
123
Example 2
public class JavaIntegerParseIntExample2 {
    public static void main(String[] args) {
        String s="123";
        int radix=9;
        //parses the string argument as a signed integer in the radix specified by the second argument
        int val=Integer.parseInt(s,radix);
        System.out.println(val);
    }
}
Output
102
Example 3
public class JavaIntegerParseIntExample3 {
    public static void main(String[] args) {
        String s="123";
        //radix should be smaller than Character.MAX_RADIX i.e. 36 else it will give you an error
        int radix=37;
        //parses the string argument as a signed integer in the radix specified by the second argument
        int val=Integer.parseInt(s,radix);
        System.out.println(val);
    }
}
Output
Exception in thread "main" java.lang.NumberFormatException: radix 37 greater than Character.MAX_RADIX

              at java.lang.Integer.parseInt(Integer.java:551)

              at com.TutorialAndExample.JavaIntegerParseIntExample3.main(JavaIntegerParseIntExample3.java:9)
Example 4
public class JavaIntegerParseIntExample4 {
    public static void main(String[] args) {
        //give NumberFormatException if the String does not contain a parsable int
        String s=null;
        //radix should be smaller than Character.MAX_RADIX i.e. 36 else it will give you an error
        int radix=37;
       /*It parses the CharSequence argument as a signed as a signed int in the specified radix,
        beginning at the specified beginIndex and extending to endIndex -1.*/
        int val=Integer.parseInt(s,radix);
        System.out.println(val);
    }
}
Output
Exception in thread "main" java.lang.NumberFormatException: null

              at java.lang.Integer.parseInt(Integer.java:542)

              at com.TutorialAndExample.JavaIntegerParseIntExample4.main(JavaIntegerParseIntExample4.java:10)

Related Topics

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Java Keywords

Java Keywords The particular words which are used in java programming language that act like a key or important words to write a code are called java keywords. Java Keywords are...

4 minutes read.

Types of Events in Java

One of Java's key ideas is the principle of an event. Events in Java are activities that result in a change in the state or behavior of an object. The...

4 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

Java Append Data to File

When writing data to a file using the classes within the java.io package, its file will often be overwritten, meaning that any existing data will be removed and new data...

4 minutes read.

Byte to Hex in Java

Java exclusively uses byte data types to store in a byte array, which is an array. Each component of a byte array has a default value of 0. Hex String -...

3 minutes read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

Java Tutorial

What is Java? Java is an object-oriented, robust, secured and platform-independent programming language. With the help of Java Programming, we can develop console, window, web, enterprise and mobile applications. Java language was...

22 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.

Java Try Keyword

The try block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java try block has...

3 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

3 minutes read.

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR. Truth table: XYXNOR001010100111 If the bits are the same, it returns 1, else it returns 0. Examples:  Input: 10 20 Output : 1 A Binary...

3 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Method and Block Synchronization in Java

The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by...

3 minutes read.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.