Convert String to Int in Java

Convert String to Int in Java | As we all know the string is a collection of a sequence of characters where once created it cannot be changed because the string is immutable. Now we want to change the string type to an integer type.

Here we will discuss how to convert string to int in Java. The string to int conversion can be done in the following ways:-
1. By using Integer.parseInt() method.
2. By using Integer.valueOf() method

Example:-
1. String: “123456”
int value: 123456
2. String = “KnowProgram30”
Invalid string.

Convert Java String to Int Using Integer.parseInt()

The parseInt() is one of the methods to convert String to int in Java. The method details of parseInt() are as follows:- public static int parseInt(String s)throws NumberFormatException

The parseInt() method takes a String containing the int representation to be parsed and returns the integer value represented by the argument in decimal. It throws NumberFormatException if the string does not contain a parsable integer.

public class Main {
   public static void main(String[] args) {
      String string = "89564";
      try {
         int value = Integer.parseInt(string);
         System.out.println("Integer value = " + value);
      } catch (NumberFormatException nfe) {
         System.out.println(nfe.toString());
      }

      string = "KnowProgram2030";
      try {
         int value = Integer.parseInt(string);
         System.out.println("Integer value = " + value);
      } catch (NumberFormatException nfe) {
         System.out.println(nfe.toString());
      }
   }
}

Output:-

Integer value = 89564
java.lang.NumberFormatException: For input string: “KnowProgram2030”

Since the parseInt() method throws NumberFormatException therefore to handle the exception we have used the try-catch block. To display the exception message we have used toString() method but you can also use printStackTrace() or getMessage() method. See more:- Different ways to get the exception message

Convert String to Int in Java using Integer.valueOf()

Java Integer class also contains the valueOf() method, which can be used to convert a string to an int in Java. Internally valueOf() method calls parseInt() method. Therefore it is very similar to the parseInt() method. The method is defined as follows:- public static Integer valueOf(String s) throws NumberFormatException

It takes a string that needs to be converted to an int value and returns an Integer object holding the value of the specified String. Since parseInt() method throws NumberFormatException therefore due to exception propagation valueOf() of Integer class also throws NumberFormatException.

public class Main {
   public static void main(String[] args) {
      String string = "123456";
      try {
         int value = Integer.valueOf(string);
         System.out.println("Integer value = " + value);
      } catch (NumberFormatException nfe) {
         System.out.println(nfe.toString());
      }
      
      string = "Hello";
      try {
         int value = Integer.valueOf(string);
         System.out.println("Integer value = " + value);
      } catch (NumberFormatException nfe) {
         System.out.println(nfe.toString());
      }
   }
}

Output:-

Integer value = 123456
java.lang.NumberFormatException: For input string: “Hello”

Convert String To Int Java Example

Now let us see some use cases of converting string to int in Java. We will take 2 strings from the user, convert them into int values using one of the above two methods and return the addition of two numbers. If the string is not parsable to int then it will throw NumberFormatException.

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter string1: ");
      String str1 = scan.next();
      System.out.print("Enter string2: ");
      String str2 = scan.next();
      System.out.println("Addition value: " 
                        + add(str1, str2));
      scan.close();
   }

   public static int add(String str1, String str2) {
      try {
         int num1 = Integer.parseInt(str1);
         int num2 = Integer.parseInt(str2);
         return num1 + num2;
      } catch (NumberFormatException nfe) {
         throw nfe;
      }
   }
}

Output:-

Enter string1: 10
Enter string2: 20
Addition value: 30

Enter string1: 10
Enter string2: a
Exception in thread “main” java.lang.NumberFormatException: For input string: “a”

Enter string1: a
Enter string2: 99
Exception in thread “main” java.lang.NumberFormatException: For input string: “a”

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.