How to convert String to int or Integer, int to string in java


In this tutorial we will learn How to convert String to int or Integer in Java with program and examples. Read when to convert to primitive type i.e int and wrapper classes i.e Integer in java. We will take care of exception handling while performing the above conversion as well, we will convert String to int using NumberUtils in java which is class from apache commons library. We will also learn how to convert StringBuffer to int in Java. We will also convert String to int in Java manually/i.e. without using java library with exception handling.




How to convert String to int in Java >
  • Program/Example 1 to convert String to int in Java
  • Program/Example 2 to convert String to Integer in Java
  • Program/Example 3 to convert String to int in Java with exception handling.
  • Program/Example 4 to convert String to int using NumberUtils in java which is class from apache library.
  • Program/Example 5 to convert String to int in Java manually/i.e. without using java library with exception handling.



Program/Example 1 to convert String to int in Java
Integer.parseInt(string) method returns int value of String
public class ConvertStringToIntInJavaExample {
   public static void main(String[] args) {
          String string = "123";
          int i = Integer.parseInt(string);
   }
}




Program/Example 2 to convert String to Integer in Java
Integer.valueOf(string) method returns Integer object.
public class ConvertStringToIntegerInJavaExample {
   public static void main(String[] args) {
          String string = "123";
          Integer i = Integer.valueOf(string);
   }
}

Integer.valueOf(string) method always returns cached Integer object. Read How Java caches Integer objects formed from primitive int values between values -128 to 127



Program/Example 3 to convert String to int in Java with exception handling.
When you convert some string like “123a” to int it will throw NumberFormatException in java, you must catch it.
public class ConvertStringToIntWithExceptionHandlingExample {
   public static void main(String[] args) {
          String string = "123a";
          try {
                 int i = Integer.parseInt(string);
          } catch (NumberFormatException e) {
                 e.printStackTrace();
          }
   }
}

Output of above program -
java.lang.NumberFormatException: For input string: "123a"
   at java.lang.NumberFormatException.forInputString(Unknown Source)
   at java.lang.Integer.parseInt(Unknown Source)
   at java.lang.Integer.parseInt(Unknown Source)
   at ConvertStringToIntWithExceptionHandlingExample.main(ConvertStringToIntWithExceptionHandlingExample.java:8)



Program/Example 4 to convert String to int using NumberUtils in java which is class from apache library.

NumberUtils.stringToInt(string) method returns int value of String.
import org.apache.commons.lang.NumberUtils;

public class ConvertStringToIntUsingNumberUtilsExample {
   public static void main(String[] args) {
          String string = "123";
          int i = NumberUtils.stringToInt(string);
   }
}



Program/Example 5 to convert String to int in Java manually/i.e. without using java library with exception handling.
In this example we will convert String to int in Java manually. We will also check whether number is numeric or not.
package StringToInt;
public class ConvertStringToIntWithoutUsingJavaLibraryExample {
   public static void main(String[] args) {
          String string = "123";
          int n = convertStringToInteger(string);
          System.out.println(n);
   }
  
   public static int convertStringToInteger( String string ){
   int i = 0;
   int n = 0;
   boolean numberIsNegative = false;
   //check number is negative or not
   if (string.charAt(0) == '-') {
     numberIsNegative = true;
       i++;
   }
   while( i < string.length()) {
       n *= 10;
      
       //Check character is numeric
       //LOGIC = ASCII value of 0 is 48, ASCII value of 9 is 57
       if( string.charAt(i)< 48 || string.charAt(i) >57 ){
         throw new NumberFormatException("String is not numeric");
       }
      
     //ASCII value of 1 is 49, 2 is 50 and so on.
       //Example 1 = for char value 1, ASCII value is 49,
       //              So storing char value 1 (i.e. ASCII value 49) in int will result in 49.
       //              subtracting 48 from char value 1 (i.e. ASCII value 49) will store 1 in int.
       //Example 2 = for char value 2, ASCII value is 50,
       //              So storing char value 2 (i.e. ASCII value 50) in int will result in 50.
       //              subtracting 48 from char value 2 (i.e. ASCII value 50) will store 2 in int.
       n += string.charAt(i) - 48;
       i++;
   }
   if (numberIsNegative)
       n = -n;
  
   return n;
   }
}
/*
*/




Now, in this core java tutorial we will learn how to convert int primitive type to String in java and convert Integer object to String with program and examples in java.

Programs to convert int to String in java -
  • Program/Example 6 to How to convert int primitive type to String  in java.
  • Program/Example 7 to How to convert Integer object to String in java

Program/Example 6 to How to convert int primitive type to String  in java.
String.valueOf(int) method converts int primitive type to String  in java.

package convertIntToStringPkg;
public class ConvertIntToStringExample1 {
   public static void main(String[] args) {
          int i = 12;
          String string = String.valueOf(i);
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12

*/



Program/Example 7 to How to convert Integer object to String in java
toString() method of java.lang.Integer class convert int object to String in java.

package convertIntToStringPkg;
public class ConvertIntObjectToStringExample2 {
   public static void main(String[] args) {
          Integer integerObject = 12;
          String string = integerObject.toString();
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12
*/




Summary -
So in this tutorial we learned How to convert String to int or Integer in Java with program and examples. Read why When to use primitive type i.e int and wrapper classes i.e Integer in java. We took care of exception handling while performing the above conversion as well, we converted String to int using NumberUtils in java which is class from apache library. We also learned how to convert StringBuffer to int in Java. We also converted String to int in Java manually/i.e. without using java library with exception handling.

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

How to convert ArrayList to HashSet and Set to Array in Java examples

convert Array to Set and Set to array in Java


Collection - List, Set and Map all properties in tabular form


No comments:

Post a Comment