Java parseInt() Method: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-04 04:02:31   8285  Share
Subject Syllabus DetailsSubject Details 15 Program
☰ TContent
☰Fullscreen

Table of Content:

Description

This method is used to get the primitive data type of a certain String. parseXxx() is a static method and can have one argument or two.

Syntax

Following are all the variants of this method ?

static int parseInt(String s)
static int parseInt(String s, int radix)

Parameters

Here is the detail of parameters ?

  • s ? This is a string representation of decimal.

  • radix ? This would be used to convert String s into integer.

Return Value

  • parseInt(String s) ? This returns an integer (decimal only).

  • parseInt(int i) ? This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.

Throws

NumberFormatException : if the string does not contain a parsable integer.

Example

public class ParseIntMethod {

   public static void main(String args[]) {
      int x =Integer.parseInt("18");
      double c = Double.parseDouble("12");
      int b = Integer.parseInt("444",16);

      System.out.println(x);
      System.out.println(c);
      System.out.println(b);
   }
}

This will produce the following result ?

Output

18
12.0
1092
Press any key to continue . . .

Example

public class ParseIntMethod {
public static void main(String[] args)
    {
        // parsing different strings
        int z = Integer.parseInt("654",8);
        int a = Integer.parseInt("-FF", 16);
        long l = Long.parseLong("2158611234",10);

        System.out.println(z);
        System.out.println(a);
        System.out.println(l);

        // run-time NumberFormatException will occur here
        // "Hello" is not a parsable string
        int x = Integer.parseInt("Hello",8);

        // run-time NumberFormatException will occur here
        // (for octal(8),allowed digits are [0-7])
        int y = Integer.parseInt("99",8);

    }
}

This will produce the following result ?

Output

428
-255
2158611234
Exception in thread "main" java.lang.NumberFormatException: For input string: "Hello"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at ParseIntMethod.main(ParseIntMethod.java:15)
Press any key to continue . . .

Example

public class ParseIntMethod {
public static void main(String[] args)
    {
        // parsing different strings
        int z = Integer.parseInt("654");
        long l = Long.parseLong("2158611234");

        System.out.println(z);
        System.out.println(l);

        // run-time NumberFormatException will occur here
        // "Hello" is not a parsable string
        int x = Integer.parseInt("Hello");

        // run-time NumberFormatException will occur here
        // (for decimal(10),allowed digits are [0-9])
        int a = Integer.parseInt("-FF");

    }
}

This will produce the following result ?

Output

654
2158611234
Exception in thread "main" java.lang.NumberFormatException: For input string: "Hello"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at java.lang.Integer.parseInt(Integer.java:615)
        at ParseIntMethod.main(ParseIntMethod.java:13)
Press any key to continue . . .