2

Hello fellow programmers ! I am a beginner with Java and i am looking for a method or a way maybe to store the digits of a 6 digit number entered by the user , in an int array. For example :- if the number is 675421. then i want to store the digits in an array like :-

int[] array = new int[6];
int number = 675421 
array[0] = 6;
array[1] = 7;
array[2] = 5; 
array[3] = 4;
array[4] = 2;
array[5] = 1;

I want to do so so that i can work with the array to maybe sort or change the order or numbers in array. Thanks!

m0mosenpai
  • 67
  • 3
  • 10
  • 1
    Possible duplicate of [How to split an Integer into an Array using Java?](http://stackoverflow.com/questions/26319579/how-to-split-an-integer-into-an-array-using-java) – sinclair Jan 30 '16 at 18:37
  • @Mr.Momo what is your question? – Faraz Jan 30 '16 at 18:42
  • Oh im really sorry if it is a duplicate....its just that i didnt quite understand what to do in this case . so if anyone can explain to me the thing to be done and the logic behind it ? – m0mosenpai Jan 30 '16 at 18:42
  • 1
    My question is how to reproduce such an array from a given number or a number entered by the user. I want to separately store the digits in a array – m0mosenpai Jan 30 '16 at 18:43
  • Have a look at this : http://stackoverflow.com/a/8033593/4272498 – Irfan Jan 30 '16 at 18:44

5 Answers5

2

Here you go,

String temp = Integer.toString(number);

int[] num = new int[temp.length()];

for (int i = 0; i < temp.length(); i++){
    num[i] = temp.charAt(i) - '0';
}

for (int i = 0; i < temp.length(); i++) {
    System.out.println(num[i]);
}

Edit, after comment

Here, First, you are converting to your number to a string. Then, take each char out of it(in the loop), subtract the ASCII value of 0 from each char to get the digit [ie, ASCII of 0 is 48, 1 is 49, ... ] (see ASCII table)

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
1

Do something like this:

String number = "123123";
    int[] intArray = new int[number.length()];

    for (int i = 0; i < number.length(); i++)
    {
        intArray[i] = Integer.parseInt(Character.toString(number.charAt(i)));
    }

Hope this helps,

Jason.

asdfasdfadsf
  • 381
  • 3
  • 14
1

Below is the recursive solution

public static void main(String[] args) {
    int testNum = 675421;
    List<Integer> digitList = new ArrayList<Integer>();
    collectDigits(testNum, digitList);
    Object[] resultArr = digitList.toArray();
    int listSize = resultArr.length;
    for (int listCount = 0; listCount < listSize; listCount++) {
        System.out.println("result["+listCount+"] = "+resultArr[listCount]);
    }
}

private static void collectDigits(int num, List<Integer> digits) {
    if (num / 10 > 0) {
        collectDigits(num / 10, digits);
    }
    digits.add(num % 10);
}
Tarun Jain
  • 262
  • 1
  • 8
0

One way to do this would be to turn the original integer into a string. Loop over the string, parsing each character back to an int, and place into the array. Here is an example:

int number = 123456;

String strNumber = number+"";

int[] array = new int[strNumber.length()];

int index = 0;

for(char c : strNumber.toCharArray()){
    array[index++] = Integer.parseInt(c+"");
}

System.out.println(Arrays.toString(array));
jheimbouch
  • 969
  • 8
  • 20
0

Math solution, you can split the int number using this:

int[] array = new int[6];
int number = 675421;
array[0] = ((number/100000)%10);
array[1] = ((number/10000)%10);
array[2] = ((number/1000)%10);
array[3] = ((number/100)%10);
array[4] = ((number/10)%10);
array[5] = ((number/1)%10);

If the "number" has a variable length you can automate this, write a coment if you need help

Andrea Catania
  • 1,361
  • 3
  • 21
  • 37