In this article, we will discuss different ways to convert the String to int in Java.
Here, String refers to String representation of an Integer.
For example, given the String “12”, the result should be the Integer 12.
Here are the different approaches for converting String to int in Java :
1. Using Integer.parseInt()
We can convert a String to an int using Integer class’s parseInt() method.
String str = "1";
int num = Integer.parseInt(str);
Similarly, Byte, Short, Long, Float, Double classes provide following methods :
Byte.parseByte()
Short.parseShort()
Long.parseLong()
Float.parseFloat()
Double.parseDouble()
These methods throw NumberFormatException if String cannot be converted to the corresponding number format.
package com.topjavatutorial; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class StringtoIntProgram1 { public static void main(String[] args) throws IOException { System.out.println("Enter a number"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); try{ int number = Integer.parseInt(str); System.out.println("Entered number is " + number); } catch(NumberFormatException e){ System.out.println(str + " is not an integer"); } } }
Output
Enter a number
10
Entered number is 10
Enter a number
abc
abc is not an integer
2. Using Integer.valueOf()
We can also use valueOf() method to convert a String to anSInteger.
String str = "1";
int num = Integer.valueOf(str);
Similarly, Byte, Short, Long, Float,Double classes also provide valueOf() method for converting a String to respective numeric format.
valueOf() throw NumberFormatException if String cannot be converted to the corresponding number format.
package com.topjavatutorial; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class StringtoIntProgram2 { public static void main(String[] args) throws IOException { System.out.println("Enter a number"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); try{ int number = Integer.valueOf(str); System.out.println("Entered number is " + number); } catch(NumberFormatException e){ System.out.println(str + " is not an integer"); } } }
Output
Enter a number
10
Entered number is 10
Enter a number
abc
abc is not an integer
What is the difference between valueOf() and parseInt() methods ?
The valueOf() method returns an Integer object whereas parseInt() returns a primitive int value.
3. Using Integer.decode()
Integer Class’s decode() method can also convert a String into an Integer.
String str = "12"; int val; try { val = Integer.decode(str); System.out.println(val); } catch (NumberFormatException e) { System.out.println("Not a number"); }
Output:
12
4. Using Integer constructor
We can use Integer constructor mentioned below to create a new Integer object that represents the int value indicated by the String parameter.
public Integer(String s)
throws NumberFormatException
For example :
Integer val = new Integer("12");
5. Using Apache Commons NumberUtils class
If using Apache Commons, you may also use one of the following methods in NumberUtils class to convert a String to an int:
NumberUtils.createInteger(String str)
NumberUtils.toInt(String str)
In this article, we saw different ways of converting a String to an int or Integer.
If you were looking for an int, but the method returns an Integer, you can always unbox it or use the intValue() method. Also, most of the methods refered above throw NumberFormatException..so, you should handle this exception so that the code doesn’t break when an invalid string is passed.
© 2016 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post
#
Nice one.