Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,860 questions

51,781 answers

573 users

How to convert an int number into an array of int digits in Java

2 Answers

0 votes
public class Main {
    static int[] intNumberIntoIntDigitsArray(int number) { 
        String numberStr = Integer.toString(number);
        int[] digits = new int[numberStr.length()];
            
        for (int i = 0; i < numberStr.length(); i++) {
            digits[i] = Character.getNumericValue(numberStr.charAt(i));
        }
       
        return digits;
    } 
    public static void main(String[] args) {
        int number = 12345;
        int[] digits = intNumberIntoIntDigitsArray(number);

        for (int digit : digits) {
            System.out.print(digit + " ");
        }
    }
}

   
   
/*
run:
   
1 2 3 4 5 
   
*/

 



answered Jan 7, 2025 by avibootz
0 votes
import java.util.ArrayList;
import java.util.Collections;

public class Main {
    static int[] intNumberIntoIntDigitsArray(int number) { 
        ArrayList<Integer> digitList = new ArrayList<>();
        
        while (number > 0) {
            digitList.add(number % 10);
            number /= 10;
        }
        
        Collections.reverse(digitList);
        int[] digits = digitList.stream().mapToInt(i -> i).toArray();
       
        return digits;
    } 
    public static void main(String[] args) {
        int number = 12345;
        int[] digits = intNumberIntoIntDigitsArray(number);

        for (int digit : digits) {
            System.out.print(digit + " ");
        }
    }
}


   
   
/*
run:
   
1 2 3 4 5 
   
*/

 



answered Jan 7, 2025 by avibootz

Related questions

2 answers 75 views
2 answers 232 views
2 answers 264 views
1 answer 73 views
1 answer 153 views
1 answer 146 views
...