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!

5
  • 1
    Possible duplicate of How to split an Integer into an Array using Java? Commented Jan 30, 2016 at 18:37
  • @Mr.Momo what is your question? Commented Jan 30, 2016 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 ? Commented Jan 30, 2016 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 Commented Jan 30, 2016 at 18:43
  • Have a look at this : stackoverflow.com/a/8033593/4272498 Commented Jan 30, 2016 at 18:44

5 Answers 5

4

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ! can u explain me the line within the first for loop?
Answer Updated, hope it helps you.
2

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.

Comments

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);
}

2 Comments

A bit complicated for me but thanks anyways. Appreciate the help.
I have divided the number by 10, store the remainder in an array, call collectDigits function with quotient as the new number
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));

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.