Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JAVA»How to Convert String to int in Java

    How to Convert String to int in Java

    RahulBy RahulFebruary 26, 20172 Mins ReadUpdated:August 3, 2018

    Java is an object orientied programming languege. In Java you can use Integer.parseInt() or Integer.valueOf() functions to convert String value to an int value. In any case String is not a convertible then NumberFormatException will occur.

    #1. Using Integer.parseInt()

    Integer.parseInt() converts string value and returns an int primitive as result. For below example, first, we store 5 as a string value to id variable. Then convert it to integer and save to result variable.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    class ConvertStringToInt1 {
     
    public static void main(String args[]){
    String id = "5";
    int result = Integer.parseInt(id);
    System.out.println(result);
    }
    }

    Save the above content in ConvsertStringToInt1.java file and compile and run this program.

    Compile:

    javac ConvertStringToInt1.java
    

    Run:

    java ConvertStringToInt1
    
    5
    

    #2. Using Integer.valueOf()

    Integer.valueOf() also uses Integer.parseInt function in backend but in result, it provides and Integer object value.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    class ConvertStringToInt2 {
     
    public static void main(String args[]){
    String id = "5";
    Integer result = Integer.valueOf(id);
    System.out.println(result);
    }
    }

    Save the above content in ConvsertStringToInt2.java file and compile and run this program.

    Compile:

    javac ConvertStringToInt2.java
    

    Run:

    java ConvertStringToInt2
    
    5
    

    #3. Exception in Conversion

    If the given input is not parsable by the any of the above methods, NumberFormatException will be thrown. As in the below example, we are storing string”5abc” in id variable which contains alphabets. If we try to convert this number to int, NumberFormatException exception will occur.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class ConvertStringToInt3 {
     
    public static void main(String args[]) {
    String id = "5abc";
    try{
    int result = Integer.parseInt(id);
    System.out.println(result);
    }catch(NumberFormatException e){
    System.out.println(e);
    }
    }
    }

    Save the above content in ConvsertStringToInt3.java file and compile and run this program.

    Compile:

    javac ConvertStringToInt3.java
    

    Run:

    java ConvertStringToInt3
    
    java.lang.NumberFormatException: For input string: "5abc"
    

    Reference:

    http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html

    int Java string
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Set JAVA_HOME on Windows 7/8/10
    Next Article How to Mount and Unmount Filesystem in Linux

    Related Posts

    How to Replace String in JavaScript

    Updated:June 13, 20222 Mins Read

    How to Install JAVA on Ubuntu 22.04

    Updated:May 16, 20224 Mins Read

    Java HashMap – How to Get Value from Key

    Updated:April 6, 20222 Mins Read

    Java HashMap – How to Get Key from Value

    Updated:April 6, 20222 Mins Read

    5 Methods to Print an Array in Java

    Updated:April 21, 20222 Mins Read

    How To Install Java on Debian 11

    4 Mins Read

    1 Comment

    1. kavya on May 10, 2018 9:24 am

      How to convert string to int in java 8
      In Java 8 using stream API we can convert set of String to set of Integer, it involves following steps like,

      1. The Set.Stream() is used to convert set to stream.

      2. The Steam.map() is used to convert stream to stream.

      3. The Collectorts.toset() is used to Accumulate stream to set.

      4. The map() operation is used to throw NumberFormatException, if string does not contains parsable integer.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.