Automation Using Selenium Webdriver

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

Tuesday, 15 November 2016

Java Program to find missing numbers in an array

  • To find missing numbers in an array first we need to make sure that array is sorted.
  • After sorting we need to check that array each element with next element then we can find the difference.
  • if Array is not sorted :To sort array use Arrays.sort(array);
  • If difference is 1 then no need to do any thing because numbers are in order.
  • If difference is not equal to 1 then we need to print all those numbers or pick those numbers and place it in one array.
  • this would be the logic to find missing numbers in an array
  • Here there may be a chance of array not starts with 1 then we need to check first itself whether array starts with 1 or not if not we need to print 1 to starting element of array.
  • for example int a[]={4,5,6,8}; then we need to print 1 2 3  7.
Lets see a java example program to find missing numbers in an array.


 

 Lets see a java example program to find missing numbers in an array.

  1. package arraysInterviewQuestions;
  2. public class PrintMissingNumbers {
  3.  
  4. private static void findMissingNumber(int[] number){
  5.  
  6.         // take max length as last number in array
  7.     int k[] = new int[number[number.length-1]];
  8.         
  9.   int m=0;
  10.  
  11.   if(number[0]!=1){
  12.    for (int x = 1; x < number[0]; x++) {
  13.        k[m] =  x;
  14.        m++;
  15.        }
  16.   }
  17.         
  18.  for (int i = 0; i < number.length -1; i++) {
  19.     
  20.     int j = i+1;
  21.     int difference = number[j] - number[i] ;
  22.             
  23.             
  24.    if(difference != 1 ){
  25.         
  26.   for (int x = 1; x < difference; x++) {
  27.  
  28.           k[m] = number[i] + x;
  29.            m++;
  30.     
  31. }
  32.             
  33.  }
  34.  }
  35.         
  36. System.out.println("missing numbers in array ::");
  37.         
  38. for (int l = 0; l < m ; l++) {
  39.     System.out.println( k[l]+" ");
  40. }
  41. }
  42.  public static void main(String[] args) {
  43.         
  44.    int a[]= {2,4,6,9,10,20};
  45.  
  46.    //if Array is not sorted :To sort array use Arrays.sort(a); 
  47.  
  48.   findMissingNumber(a);
  49.  
  50.    
  51. }
  52. }
 
Missing%2Bnumbers%2Bin%2Barray


  • Change the array in main method and practice try to find missing elements in an array that contains 1 to 100 numbers.

  • You Might Like

How to find uppercase letters in a string in java


  • How can we count upper case letter in String  java ?
  • How to find uppercase letters in String  java?
  • How to find capital letters in string in java?
  • How u discover the capital letter given sentence?
  • Yes We can find uppercase or capital letters in a String using isUpperCase() method of Character class in java
  • In order to find number of uppercase letters in a string of all capital letters in a string we need to iterate all characters of a string in a loop and check individual characters are uppercase letters or not using Character class provided isUpperCase() method.

Program #1: Java example program to find all capital letters / Uppercase letters in a String 



  1. package findUppercaseletters.String;
  2. public class FinfUppercaseLetters {
  3.  
  4.     /**
  5.      * 
  6.      */
  7.  
  8.  public static void main(String[] args) {
  9.         
  10.         
  11.   String str= "How to Print Uppercase Letters in Java";
  12.  
  13.     for (int i = 0; i < str.length(); i++) {
  14.     
  15.             if(Character.isUpperCase(str.charAt(i))){    
  16.             System.out.println(str.charAt(i));
  17.             }
  18.             
  19.  }
  20.  
  21. }
  22.  
  23. }

 Output:

  1. H
  2. P
  3. U
  4. L
  5. J