Convert Char Array to Int in Java

  1. Convert Char Array to Int Using the parseInt() Method
  2. Convert Char Array to Int Using parseInt() Method
  3. Convert Char Array to Int Using the Customized Code

This tutorial introduces how to convert char array to int in Java and lists some example codes to understand the topic.

An array is a container that stores elements of the same data type. For example, an integer array can have only integer type values. An array that contains char values will be called a char array. In this article, we will use several built-in methods such as parseInt(), valueOf(), String(), etc that we will find a value in the given array.

Convert Char Array to Int Using the parseInt() Method

We can use the parseInt() method to convert char array to int in Java. This method takes a String object and returns an integer value. This method belongs to the Integer class so that it can be used for conversion into an integer. See the example below.

public class SimpleTesting{
    public static void main(String[] args) {
        char[] arr = {'2','3','4','5'};
        int val = contains(arr);
        System.out.println("Int value after conversion : "+val);
    }
    public static int contains(final char[] arr) {
        int number = Integer.parseInt(new String(arr));
        return number; 
    }
}

Output:

Int value after conversion : 2345

Convert Char Array to Int Using parseInt() Method

We can use the parseInt() method and valueOf() method to convert char array to int in Java. The parseInt() method takes a String object which is returned by the valueOf() method, and returns an integer value. This method belongs to the Integer class so that it can be used for conversion into an integer. See the example below.

public class SimpleTesting{
    public static void main(String[] args) {
        char[] arr = {'2','3','4','5'};
        int val = contains(arr);
        System.out.println("Int value after conversion : "+val);
    }
    public static int contains(final char[] arr) {
        int number = Integer.parseInt(String.valueOf(arr));
        return number; 
    }
}

Output:

Int value after conversion : 2345

Convert Char Array to Int Using the Customized Code

We can use the customized method to convert char array to int in Java. We create the method contains() that takes a char array as an argument and returns an integer value. You can use this example if you don’t want to use built-in methods such as parseInt(). See the example below.

public class SimpleTesting{
	public static void main(String[] args) {
		char[] arr = {'2','3','4','5'};
		int val = contains(arr);
		System.out.println("Int value after conversion : "+val);
	}
	public static int contains(final char[] arr) {
		int result = 0;
		int length = arr.length - 1;

		for (int i = 0; i <= length; i++)
		{
			int digit = arr[i] - '0';
			result *= 10;
			result += digit;
		}
		return result;
	}
}

Output:

Int value after conversion : 2345

Related Article - Java Array

  • Get Length of Array in Java
  • Check if an Array Contains Int in Java