java.lang.String Cannot Be Converted to Int

java.lang.String Cannot Be Converted to Int | Sometimes while converting values we may get “incompatible types java.lang.string cannot be converted to int”. The string is a collection of a sequence of characters that consists of a string literal enclosed within the double-quotes. The integer is the datatype that contains the number. The java.lang.String class objects are immutable and usually represent the character strings.

As we know in some scenarios we convert string to int, in this case, we might get some errors so here we are showing the errors which might occur while converting string to int. 

public class Main {
   public static void main(String[] args) {
      String num = "2025";
      System.out.println("Num: " + num);
      int value = num;
      System.out.println("Value: " + value);
   }
}

While compiling we get the following error:-

Main.java:5: error: incompatible types: String cannot be converted to int
int value = num;
^
1 error

Since int is a primitive data type and string is a referenced data type, both are incompatible; hence, we can’t assign a string to an int or int to a string.

Solution for “String cannot be converted to int” Error

To solve this problem we have to use some predefined methods that are given to convert a string to an int value. These methods are Integer.parseInt() and Integer.valueOf(). See more:- Convert String to Int in Java

import java.util.Scanner;

public class Main {
   public static void main(String args[]) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter string: ");
      String string = scan.next();

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

      scan.close();
   }
}

Output:-

Enter string: 12345
Number: 12345

Enter string: 8485.652
java.lang.NumberFormatException: For input string: “8485.652”

Enter string: KnowProgram
java.lang.NumberFormatException: For input string: “KnowProgram”

The string “12345” is a valid parsable string for int type, therefore it is converted to an int value. But “8485.652” will be double type after parsing, and we can’t assign double to int. To parse “8485.652” we have to use Double.parseDouble() method. In the 3rd test case “KnowProgram” is not a parable string therefore we can’t convert them into the int and Integer.parseInt() method throws NumberFormatException.

In a very similar way, we can also use Integer.valueOf() method. This method internally calls Integer.parseInt() method and also throws NumberFormatException because of exception propagation whenever a string is not parsable to int.

import java.util.Scanner;

public class Main {
   public static void main(String args[]) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter string: ");
      String string = scan.next();
      
      try {
         int num = Integer.valueOf(string);
         System.out.println("Number: " + num);
      } catch (NumberFormatException nfe) {
         nfe.printStackTrace();
      }
      scan.close();
   }
}

Output:-

Enter string: 20
Number: 20

Enter string: KnowProgram2025
java.lang.NumberFormatException: For input string: “KnowProgram2025”
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:660)
at java.base/java.lang.Integer.valueOf(Integer.java:991)
at Main.main(Main.java:10)

In the first example, we have used the toString() method and in the previous example, we have used the printStackTrace() method to display the exception message. There are various ways to display the exception message. See more:- Different ways to get the exception message

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. Required fields are marked *