{

How do convert string to Integer or Integer to String in java?


Convert String to Integer or Integer to String in java

As a developer, in day-to-day programming, we have encountered many situations to convert from String to Integer or vice versa.

The string object holds a sequence of characters enclosed in double-quotes.

Integer object stores the numeric values.

The string should hold a valid int value. if an invalid numeric value exists in the string, then NumberFormatException is thrown. For this, you have to handle using try and catch blocks.

How to Convert String to Integer in Java

There are several ways to convert from String to integer. For this,

Using Constructor

It is a simple and easy way to convert to Integer. First, Create an Integer object with the constructor passing String type.

String s="123";  
Integer i=new Integer(s);  

This conversion happens at object creation time.

Using Integer.parseInt() method

Integer class has a parseInt method. We have used Integer.parseInt() method to convert String to Integer.

It returns the primitive int value.

Syntax:

public static int parseInt(String s) throws NumberFormatException 

Arguments: Numerical value in the form of string Throws: It throws NumberFormatException if the non-numerical string is passed.

public class Test {
    public static void main(String[] args) {
       String s="123";    
       Integer i=Integer.parseInt(s);
    }
}

if an string contains non numerical value(abc), It thrown an error Exception in thread “main” java.lang.NumberFormatException.

public class Test {
    public static void main(String[] args) {
        int result;
        String s="abc";

        try {
            result = Integer.parseInt(s);
        }
        catch (NumberFormatException e){
            result = 0;
        }
    }
}

using valueOf() method

another way is using valueOf of the Integer object

This returns java.lang.Integer object

Here is a syntax

public static Integer valueOf(String s) throws NumberFormatException 

Arguments: Numerical value in the form of string Throws: It throws NumberFormatException if the non-numerical string is passed.

public class Test {
    public static void main(String[] args) {
        String s="123";
        Integer i=Integer.valueOf(s);
    }
}

Here is an example of a non-numerical string value

public class Test {
    public static void main(String[] args) {
        String s = "abc";
        Integer result;
        try {
             result= Integer.valueOf(s);
        } catch (NumberFormatException e) {
            System.out.println("Invalid integer value in String " + s
                    + "exception=" + e);
            result=0;
        }
    }
}

Using NumberUtils from apache commons library

if your project is using the apache commons library, org.apache.commons.lang3.math.NumberUtils class provides numeric utility functions.

It Has a toInt method which returns an integer if the input is a valid numeric value, else returns 0.

import org.apache.commons.lang3.math.NumberUtils;
public class App {
    public static void main( String[] args ){
        String s = "546";
        int result = NumberUtils.toInt(s);

    }
}

You can use NumberUtils class.

How to Convert Integer to String in Java?

Multiple ways we can convert Integer to String in java

  • String valueOf() method
  • Integer.toString() method
  • Append empty String

valueOf() method in String

It returns the String version of the input integer and returns an integer.

Here is an example

public class App {
    public static void main( String[] args ){
        int i = 1234;
        String str = String.valueOf(i);

    }
}

toString method

The toString method converts a numeric value into a string.

Here is an example

public class App {
    public static void main( String[] args ){
        int num = 1334;
        String result = Integer.toString(num);
    }
}

Output:

1334

if a string with a non-numerical value pass to the toString method, It throws an error java: incompatible types: java. lang.String cannot be converted to int. Here is an example

public class App {
    public static void main( String[] args ){
        String result = Integer.toString("num");
    }
}

Output:

java: incompatible types: java.lang.String cannot be converted to int

Append empty String

We can convert to String method using append empty string. Add an integer with an empty string using the plus operator and returns the string.

public class App {
    public static void main( String[] args ){
        Integer i=123;
        String s=""+i;
    }
}
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.