Automation Using Selenium Webdriver
Showing posts with label 8 different ways to convert int to String in java. Show all posts
Showing posts with label 8 different ways to convert int to String in java. Show all posts

Wednesday 16 November 2016

8 different ways to convert int to String in java


  • How to convert string to int or integer to string in java?
  • When we are working on a project we will be having some scenarios of expecting a string from int or integer from string.
  • Now we will be discussing on how to convert Integer to String in java with example java programs.

8 different ways to convert int to String in java

1.Convert Integer to String using Integer.toString() method: 
2.Convert Integer to String using String.valueOf() method.
3.Convert Integer to String / int to String using new Integer(int).toString() 
4.Convert Integer to String / int to String using String.format() method 
5.Convert Integer to String / int to String using DecimalFormat 
6.Convert Integer to String/ int to String using StringBuffer / StringBuilder 
7.Convert Integer to String / int to String directly by adding to "" 
8.Convert Integer to String / int to String using Special radix. 


1.Convert Integer to String using Integer.toString() method: 

  •  Integer is a wrapper class in java which is mainly used to represent or convert primitive int value to object.
  • And Integer class having some predefined methods.
  • Integer class has special predefined method to convert integer value to string toString();
  • toString is a static method in Integer class so that by using class name itself we can call that method to convert Integer to corresponding string value.
Program #1: Java Example program to convert Integer to String by using toString() method: 


 Output:


  1. package convertintegertostring.java;
  2. public class IntegerToString {
  3.  
  4.     /**
  5.      * @ website: http://corejavawithselenium.blogspot.in/
  6.      */
  7. public static void main(String[] args) {
  8.         
  9.         
  10.  Integer i = new Integer(64);
  11.         
  12.  //converting integer to string by calling toString() method on integer object
  13.  
  14.   String str= i.toString();
  15.   // also we can use like String str= Integer.toString(i);

  16.  System.out.println(str);
  17.  
  18. }
  19.  
  20. }
 Output:


  1. 64

2.Convert Integer to String using String.valueOf() method.

  •  Integer class have toString() method to convert integer to string value same way String class also has valueOf() method to convert int or integer to string.
  • String class has a static method valueOf() which takes int as argument and converts into string.

Program #2: Java Example program to convert int to string using valueOf() method. 

  1. package convertintegertostring.java;
  2. public class IntegerToString {
  3.  
  4.     /**
  5.      * @ website: http://corejavawithselenium.blogspot.in/
  6.      */
  7. public static void main(String[] args) {
  8.  
  9.   Integer i = new Integer(64);       
  10.  
  11.  //converting integer to string by calling valueOf() method on String clas
  12.   String str= String.valueOf(i);
  13.         
  14.   System.out.println(str);
  15.         
  16.         
  17.   int number=123;
  18.   String numberstr= String.valueOf(number);
  19.         
  20.   System.out.println(numberstr);     
  21.      
  22. }
  23.  
  24. }
 Output:


  1. 64
  2. 123


3.Convert Integer  / int to String using new Integer(int).toString()
  •  And here by creating Integer object and directly calling tostring method.
  • We can convert into to string directly with one statement this will be done by using Integer(int).toString()


Program #3: Java Example program to convert int to string using new Integer(int ).toString() 

  1. package convertintegertostring.java;
  2. public class IntegerToString {
  3.  
  4.     /**
  5.      * @ website: http://corejavawithselenium.blogspot.in/
  6.      */
  7. public static void main(String[] args) {
  8.  
  9.   int number=23;
  10.  
  11.  String numberstr=    new Integer(number).toString();
  12.         
  13.  System.out.println(numberstr);
  14.      
  15. }
  16.  
  17. }
 Output:


  1. 23

4.Convert Integer  / int to String using String.format() method

  •  Another alternative way to convert int t string is by calling String.format method.
  • String.format ("%d", number);
  
Program #4: Java Example program to convert int to string using String.format() method


  1. package convertintegertostring.java;
  2. public class IntegerToString {
  3.  
  4.     /**
  5.      * @ website: http://corejavawithselenium.blogspot.in/
  6.      */
  7. public static void main(String[] args) {
  8.  
  9.   int number=23;
  10.  
  11.  String numberstr=   String.format ("%d", number);
  12.         
  13.  System.out.println(numberstr);
  14.      
  15. }
  16.  
  17. }
 Output:


  1. 23

 5.Convert Integer  / int to String using DecimalFormat

  •  Another alternative way to convert int t string is by java.text.DecimalFormat
  • DecimalFormat class mainly used to represent numbers in formats.
  • Decimal format class providing format() method to convert int to string with specified format.

Program #5: Java Example program to convert int to string using DecimalFormat class


  1. package convertintegertostring.java;
  2. public class IntegerToString {
  3.  
  4.     /**
  5.      * @ website: http://corejavawithselenium.blogspot.in/
  6.      */
  7. public static void main(String[] args) {
  8.  
  9.   int number=23;
  10.        
  11.   DecimalFormat obj= new DecimalFormat("#");
  12.         
  13.   String numberstr=    obj.format(number);
  14.         
  15.   System.out.println(numberstr);
  16.         
  17.         
  18. DecimalFormat objct= new DecimalFormat("#,###");
  19.         
  20. String numberstr1=    objct.format(3400);
  21.         
  22. System.out.println(numberstr1);
  23.      
  24. }
  25.  
  26. }
 Output:


  1. 23
  2. 3,400

6.Convert Integer  / int to String using StringBuffer / StringBuilder

  •  StringBuffer and StringBuilder classes also providing toString() method to convert int to string

Program #6: Java Example program to convert int to string using StringBuffer and StringBuilder


  1. package convertintegertostring.java;
  2. public class IntegerToString {
  3.  
  4.     /**
  5.      * @ website: http://corejavawithselenium.blogspot.in/
  6.      */
  7. public static void main(String[] args) {
  8.  
  9.     int number=234;     
  10.    
  11.     StringBuffer sf=new StringBuffer();
  12.     sf.append(number);
  13.     String numberstr=    sf.toString();
  14.         
  15.    System.out.println(numberstr);
  16.         
  17.         
  18.     StringBuilder sb=new StringBuilder();
  19.     sb.append(number);
  20.     String nstr=    sb.toString();
  21.             
  22.     System.out.println(nstr);     
  23. }
  24.  
  25. }
 Output:


  1. 234
  2. 234

7.Convert Integer  / int to String directly by adding to ""

  • We can convert a number to string simply by concatenating to "".
  • But it is not recommended to use.

Program #7: Java Example program to convert int to string by concat to ""



  1. package convertintegertostring.java;
  2. public class IntegerToString {
  3.  
  4.     /**
  5.      * @ website: http://corejavawithselenium.blogspot.in/
  6.      */
  7. public static void main(String[] args) {
  8.  
  9.   int number=8538;
  10.  
  11.  String numberstr=   ""+number;
  12.         
  13.  System.out.println(numberstr);
  14.      
  15. }
  16.  
  17. }
 Output:


  1. 8538

8.Convert Integer  / int to String using Special radix.


Program #8: Java Example program to convert int to string using special radix


  1. package convertintegertostring.java;
  2. public class IntegerToString {
  3.  
  4.     /**
  5.      * @ website: http://corejavawithselenium.blogspot.in/
  6.      */
  7. public static void main(String[] args) {
  8.  
  9.  int number=536;
  10.  
  11.    String binaryString = Integer.toBinaryString(number);
  12.    System.out.println(binaryString);
  13.         
  14.    String octalString = Integer.toOctalString(number);
  15.    System.out.println(octalString);
  16.     
  17.    String hexString = Integer.toHexString(number);
  18.    System.out.println(hexString);
  19.         
  20.    String customString = Integer.toString(number, 7);
  21.    System.out.println(customString);
  22.  

  23. }
 Output:


  1. 1000011000
  2. 1030
  3. 218
  4. 1364

8 different ways to convert int to string in java



  1. 64
  2. 64
  3. 536
  4. 536
  5. 3,400
  6. 536
  7. 536
  8. 1000011000