BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Integer parseInt()Method

Last Updated: October 27, 2022 by Chaitanya Singh | Filed Under: java

Java Integer parseInt() method returns int value after parsing the given string using the specified radix. If radix is not provided, then it uses 10 as radix.

Syntax of parseInt() method

public static int parseInt(String s)
 throws NumberFormatException

public static int parseInt(String s, int radix)
 throws NumberFormatException

//this overloaded version is introduced in java 1.9
public static int parseInt (CharSequence seq, int beginIndex,
 int endIndex, int radix) 

parseInt() Parameters

  • s – String to be parsed.
  • radix – radix that is used for parsing.
  • beginIndex – The beginning index in the given character sequence. This is inclusive.
  • endIndex – The ending index in the given char sequence. This is exclusive.

parseInt() Return Value

  • An int value represented by the argument.
  • It throws NumberFormatException, if the given string or char sequence is not parsable.
  • It throws NullPointerException, if the given char sequence is null.
  • It throws IndexOutOfBoundsException, if beginIndex is negative, beginIndex > endIndex or endIndex > s.length().

Example 1: Integer.parseInt(String s)

In this example, the radix is not specified while calling parseInt() method so it parsed the strings using default radix 10 (10 is for decimal).

public class JavaExample {
  public static void main(String[] args) {
    String s1 = "100";
    String s2 = "+50";
    String s3 = "-50";
    int i1 = Integer.parseInt(s1);
    int i2 = Integer.parseInt(s2);
    int i3 = Integer.parseInt(s3);
    System.out.println("No sign int: "+i1); //default +ve
    System.out.println("Positive int: "+i2);
    System.out.println("Negative int: "+i3);
  }
}

Output: