Transform String To Int Java & Vice-Versa

Transform String To Int Java & Vice-Versa | Here we will discuss how to convert the string value to int, and how to convert int to string. The string is immutable which means cannot be changed once declared.

How To Transform String to Int Java

To convert string to int we can use the below two methods:-
1. Integer.valueOf()
2. Integer.parseInt()

The below Java program transform a string into an int by using the Integer.parseInt() method. The Integer.parseInt() method throws NumberFormatException if the given string is not a number.

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

      string = "4569h";
      try {
         value = Integer.parseInt(string);
         System.out.println("Integer value = " + value);
      } catch (NumberFormatException e) {
         System.out.println("Invalid String");
      }
   }
}

Output:-

Integer value = 2356
Invalid String

To transform string to int we can also use valueOf() method of Integer class. The valueOf() method internally calls Integer.parseInt() method and therefore it also throws NumberFormatException if the given string is not a number.

public class Main {
   public static void main(String[] args) {
      String string = "2356";
      int value = 0;
      try {
         value = Integer.valueOf(string);
         System.out.println("Integer value = " + value);
      } catch (NumberFormatException e) {
         System.out.println("Invalid String");
      }

      string = "4569h";
      try {
         value = Integer.valueOf(string);
         System.out.println("Integer value = " + value);
      } catch (NumberFormatException e) {
         System.out.println("Invalid String");
      }
   }
}

Output:-

Integer value = 2356
Invalid String

How To Transform Int To String Java

Now let us see how to transfer int to string. There are many ways to do so as listed below:-

1. By using the toString() method of Integer class
2. By using the valueOf() method of String class
3. By using the DecimalFormat class
4. By Using the StringBuilder class
5. By using the StringBuffer class

Transform int to String in Java by using the toString() method of Integer class

public class Main {
   public static void main(String args[]) {
      int num1 = 89456;
      int num2 = -12365;
      String string1 = Integer.toString(num1);
      String string2 = Integer.toString(num2);
      System.out.println("String str1 = " + string1);
      System.out.println("String str2 = " + string2);
   }
}

Output:-

String str1 = 89456
String str2 = -12365

Transform int to String in Java by using valueOf() method of String class

public class Main {
   public static void main(String args[]) {
      int num = 4165415;
      String string1 = String.valueOf(num);
      System.out.println("String = " + string1);
   }
}

Output:-

String str = 4165415

Transform int to String in Java by using DecimalFormat class

import java.text.DecimalFormat;

public class Main {
   public static void main(String args[]) {
      int num = 456987;
      DecimalFormat df = new DecimalFormat("#");
      String string = df.format(num);
      System.out.println(string);
   }
}

Output:-

456987

Transform int to String in Java by using StringBuilder class

public class Main {
   public static void main(String args[]) {
      int number = 1234;
      StringBuilder sb = new StringBuilder();
      sb.append(number);

      // convert StringBuilder to String
      String string = sb.toString();
      System.out.println("String = " + string);
   }
}

Output:-

String = 1234

Transform int to String in Java by using StringBuffer class

public class Main {
   public static void main(String args[]) {
      int number = 15345;
      StringBuffer sb = new StringBuffer();
      sb.append(number);

      // convert StringBuffer to String
      String string = sb.toString();
      System.out.println("String = " + string);
   }
}

Output:-

String = 15345

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.