×

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

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

Java Integer reverseByte() method

The reverseByte() method of Java Integer class returns the value obtained by reversing the order of the bytes in the 2’s complement binary representation. Syntax public static int reverseByte (int i) Parameters The parameter...

1 minute read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

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.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

Java Byte intValue() method

The intValue()  method of Java Integer class returns an int value for this Integer.  Syntax public int intValue()  Parameters NA  Specified by This method is specified by intValue in class Number  Return Value This method returns the numeric...

1 minute read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

What is anagram in Java?

In this section will explain what an anagram is in Java and demonstrate how to determine whether or not a text is an anagram. In Java interviews, the anagram Java...

4 minutes read.

Java Calculate Average of List

The list is a linear data structure used in Java to store ordered data collections. Additionally, it accepts duplicate values while maintaining insertion order. It is sometimes necessary to find...

3 minutes read.

Java Integer numberOfLeadingZeros() method

The numberOfLeadingZeros()  method of Java Integer class returns the total number of zero bits preceding the highest-order one-bit in the 2’s complement binary representation of the specified int value.  Syntax public static...

1 minute read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.