4

I am programming in Java and want to convert an int into an array. For example I want to convert 122 into {1, 2, 2}. Do you have an idea how this works? Thanks in advance. I am not allowed to convert it into a string.

1
  • 1
    @udiboy1209 no. There is no possibility to use string Commented Nov 12, 2017 at 22:15

3 Answers 3

3

Here is the answer without using Math class

import java.util.Arrays;
class int_to_array
{
    public static void main(String arg[])
    {
        int number = 122;
        int length=0;
        int org=number;
        while(org!=0)
        {
            org=org/10;
            length++;
        }
        int[] array = new int[length];

        for(int i = 0; i < length; i++) {
          int rem = number % 10; 
          array[length - i - 1] = rem;
          number = number / 10; 
        }

        System.out.println(Arrays.toString(array));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! I will just implement your idea and test it.
When I am implement this it seems to end up in an endless loop:
3

The following example uses pure arithmetical approach (without converting the number to string):

int number = 122;
List<Integer> digitList = new LinkedList<>();

while(number > 0) {
  int d = number % 10; //get last digit
  digitList.add(0, d);
  number = number / 10; //omit last digit
}

Integer[] digits = digitList.toArray(new Integer[0]);
System.out.println(Arrays.toString(digits));

4 Comments

Thanks, but I am not allowed to use math. functions - how can I do it without that?
Refactor the for loop to be a recursive function which skips if your 10based index is bigger than your number
Since you cannot use math you should find a method of get number of digits of the value. You can either implement your own log10 or do something like &#39;length=0;i=value;while(i:10&gt;0.9){i=i/10;length++;}'
@Z.Wo I updated the code, that doesn't use Math and length of the number.
1

This ends up in an endless loop - why that?

    int input = readInt("Bitte geben Sie eine positive Zahl ein:");
    while(input < 0) {
        input = readInt("Bitte geben Sie eine positive Zahl ein:");
    }
    int number = input;
    int length = 0;
    int org = number;
    while(org != 0) {
        org = org / 10;
        length++;
    }
    int[] inputArray = new int[length];
    int i = 0;
    while(i < inputArray.length) {
        int rem = number % 10;
        inputArray[length - i - 1] = rem;
        number = number / 10;
 i++;
    }
    String output = "{";
    i = 0;
    while(i < inputArray.length) {
        output += inputArray[i];
        if(i < inputArray.length-1) {
            output += ", ";
        }
        i++;
    }
    output += "}";
    System.out.print(output);

3 Comments

I have edited your code, check it now
This is perfect!!!! Great. Thank you so much!!!!
Please mark the answer as accepted

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.