×

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

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

PriorityBlockingQueue Class in Java

What is the Queue? An abstract data structure like Stacks is a queue. A queue is open on both ends. Data is always pushed to one end, called enqueue, and removed...

4 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Java Char Keyword

The java char keyword is a data type which is defined as character data type.Char keyword belongs to a primitive data type where the data types are classified into primitive...

4 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

Frugal number in Java

A frugal number is a positive integer with base b that has more digits than the number of prime factors it can be factored into (including exponents greater than 1)....

3 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Collection Interfaces in Java with Examples

In this tutorial, we will discuss collection interfaces in Java with Examples. Introduction The Collection Interface is an individual from the Java Collections Framework. It is a root point of interaction of...

4 minutes read.

Perfect Number Program in Java

Perfect Number Program in Java A perfect number is a number whose sum of all the factors, excluding the number itself, is equal to the number. For example, 28 is a...

4 minutes read.

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To...

4 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.