How to convert an int array to an integer in Java - Java Programming Examples

Overview

In this tutorial, we show you how to convert an int array to an integer in Java. Example: int array { 2, 0, 1, 9 } -> 2019

convert an int array to an integer in Java

Using For-each loop

package com.jackrutorial;

public class ArrayToIntegers1 {

 public static void main(String[] args) {
  int intArrs[] = { 2, 0, 1, 9 };

  System.out.print("int arrays: [");
  for (int i=0; i< intArrs.length; i++) {
   System.out.print(intArrs[i]);
   if(i < intArrs.length-1) {
    System.out.print(",");
   }
  }
  System.out.print("]");
  
  StringBuilder builder = new StringBuilder();
  for (int num : intArrs) {
   builder.append(num);
  }
  int number = Integer.parseInt(builder.toString());
  
  System.out.print(" -> converted: ");
  System.out.println(number);
 }
}

Output

int arrays: [2,0,1,9] -> converted: 2019

The way 2

package com.jackrutorial;

public class ArrayToIntegers2 {

 public static void main(String[] args) {
  int intArrs[] = {2, 0, 1, 9};
  
  System.out.print("int arrays: [");
  for (int i=0; i< intArrs.length; i++) {
   System.out.print(intArrs[i]);
   if(i < intArrs.length-1) {
    System.out.print(",");
   }
  }
  System.out.print("]");
  
  int numberConverted = 0;
  for (int number : intArrs) {
   numberConverted = 10*numberConverted + number;
  }
  
  System.out.print(" -> converted: ");
  System.out.println(numberConverted);
 }
}

Output

int arrays: [2,0,1,9] -> converted: 2019

The way 3

package com.jackrutorial;

public class ArrayToIntegers3 {

 public static void main(String[] args) {
  int intArrs[] = {2, 0, 1, 9};
  
  System.out.print("int arrays: [");
  for (int i=0; i< intArrs.length; i++) {
   System.out.print(intArrs[i]);
   if(i < intArrs.length-1) {
    System.out.print(",");
   }
  }
  System.out.print("]");
  
  int numberConverted = 0;
  for(int i = 0; i < intArrs.length; i++){
   numberConverted += intArrs[i]*Math.pow(10,intArrs.length-i-1);
     }
  
  System.out.print(" -> converted: ");
  System.out.println(numberConverted);
 }
}

Output

int arrays: [2,0,1,9] -> converted: 2019

The way 4

package com.jackrutorial;

import org.apache.commons.lang.StringUtils;

public class ArrayToIntegers4 {

 public static void main(String[] args) {
  Integer intArrs[] = { 2, 0, 1, 9 };

  System.out.print("int arrays: [");
  for (int i = 0; i < intArrs.length; i++) {
   System.out.print(intArrs[i]);
   if (i < intArrs.length - 1) {
    System.out.print(",");
   }
  }

  System.out.print("]");

  System.out.print(" -> converted: ");
  System.out.println(Integer.valueOf(StringUtils.join(intArrs)));

 }
}

Output

int arrays: [2,0,1,9] -> converted: 2019

The way 5

package com.jackrutorial;

import java.util.Arrays;

public class ArrayToIntegers5 {

 public static void main(String[] args) {
  int intArrs[] = { 2, 0, 1, 9 };

  System.out.print("int arrays: [");
  for (int i = 0; i < intArrs.length; i++) {
   System.out.print(intArrs[i]);
   if (i < intArrs.length - 1) {
    System.out.print(",");
   }
  }
  System.out.print("]");

  System.out.print(" -> converted: ");
  System.out.println(Arrays.toString(intArrs).replaceAll("\\D+",""));
 }
}

Output

int arrays: [2,0,1,9] -> converted: 2019
Previous Post
Next Post

post written by: