Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 938

Converting String to int - Java Program

$
0
0

While working on an application there are several instances when you want to convert a string to int because-

  • Your web application page takes every thing as string and later you convert values to int (if required)
  • There is a method that takes int as parameter and the value you have is String which you need to convert to int before passing it as parameter to the method.
Refer Converting int to string - Java Program to see how to convert int to string.

In Java, Integer class provides two methods for converting string to int.

  • parseInt(String s)– This method returns the integer value represented by the string argument. NumberFormatException is thrown if the string does not contain a parsable integer.
  • valueOf(String s)– This method returns an Integer object holding the value of the specified String. NumberFormatException is thrown if the string cannot be parsed as an integer.

Here Few things to note are -

  1. Both of the methods are static so you can use them directly with the class i.e. Integer.parseInt(String s) and Integer.valueOf(String s).
  2. If you have noticed parseInt() method returns int (primitive data type) where as valueOf() method returns Integer object.
  3. String passed should have digits only except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.

Converting String to int using Integer.parseInt() example code


public class StringToint {

public static void main(String[] args) {
String num = "7";
try{
int value = Integer.parseInt(num);
System.out.println("value " + value);
}catch(NumberFormatException neExp){
System.out.println("Error while conversion " +
neExp.getMessage());
}
}
}

Output


value 7

Converting String to int using Integer.valueOf() example code


public class StringToInteger {

public static void main(String[] args) {
String num = "7";
try{
Integer value = Integer.valueOf(num);
System.out.println("value " + value.intValue());
}catch(NumberFormatException neExp){
System.out.println("Error while conversion " +
neExp.getMessage());
}
}
}

Output


value 7

NumberFormatExcpetion

If conversion from String to integer fails then NumberFormatExcpetion is thrown. So, it is better to enclose your conversion code with in a try-catch block.

Example code


public class StringToint {

public static void main(String[] args) {
String num = "7abc";
try{
int value = Integer.parseInt(num);
System.out.println("value " + value);
}catch(NumberFormatException neExp){
System.out.println("Error while conversion " +
neExp.getMessage());
}
}
}

Output


Error while conversion For input string: "7abc"

That's all for this topic Converting String to int - Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Converting String to double - Java Program
  2. Converting String to Bytearray - Java Program
  3. Count Number of Words in a String - Java Program
  4. Check if Given Strings are Anagram or Not - Java Program
  5. Searching within a String using indexOf(), lastIndexOf() and contains() methods

You may also like -

>>>Go to Java Programs Page


Viewing all articles
Browse latest Browse all 938

Trending Articles