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

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:

Java Integer parseInt Output1

Example 2: Integer.parseInt(String s, int radix)

By specifying radix, we can parse a string that contains hex digits, octal digits etc. In the following example, we have a string that contains hex value, we can parse this hex value to int using radix 16.

public class JavaExample {
  public static void main(String[] args) {
    String s1 = "100";
    String s2 = "EF"; //hex value
    String s3 = "77"; //decimal: 77, octal: 63
    int i1 = Integer.parseInt(s1, 10); //radix 10 for decimal
    int i2 = Integer.parseInt(s2, 16); //radix 16 for hex
    int i3 = Integer.parseInt(s3, 8); //radix 8 for octal
    System.out.println("int value: "+i1);
    System.out.println("int value: "+i2);
    System.out.println("int value: "+i3);
  }
}

Output:

Java Integer parseInt Output2

Some more examples of parsing with radix:

//returns 0
parseInt("0", 10)

//returns 115
parseInt("115", 10)

//returns 115
parseInt("+115", 10)

//returns -115
parseInt("-115", 10)

//returns -238
parseInt("-EE", 16)

//returns 93
parseInt("1011101", 2)

//returns 1001
parseInt("1001", 10)

//returns -1001
parseInt("-1001", 10)

//this is not a valid octal number
parseInt("99", 8) throws a NumberFormatException

//this is not a valid number
parseInt("hello", 10) throws a NumberFormatException

Example 3: Integer parseInt with CharSequence and indexes

Here, we have two char sequences and we are parsing the part of the these sequences using parseInt() method of Integer class.

public class JavaExample {
  public static void main(String[] args) {
    String s = "Hello1001Bye";
    String s2 = "LEAF";
    //taking 1001 part from string s
    int i = Integer.parseInt(s, 5, 9, 10);

    //taking EA from the string s2 and using 16 to parse it as hex
    int i2 = Integer.parseInt(s2, 1, 3, 16);

    System.out.println("int value: "+i);

    //Hex "EA" is equal to 234 in decimal
    System.out.println("int value: "+i2);
  }
}

Output:

Java Integer parseInt Output3

Recommended Posts

  • Java Integer numberOfTrailingZeros()
  • Java Integer numberOfLeadingZeros()
  • Java Integer longValue()
  • Java Integer intValue()
  • Java convert String to int
❮ Java Integer

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap