-1

I need to take an inputted integer, break the digits apart, and then finally do calculations with them. I think the best way of doing this is by using an array. How do I take the int and put its digits into the array?

JavaWithJava
  • 23
  • 1
  • 3
  • [Exactly what you will need](http://stackoverflow.com/questions/3389264/how-to-get-the-separate-digits-of-an-int-number). Notice that the array can then be operated on. [1]: http://docs.oracle.com/javase/tutorial/java/data/converting.html – Dhaivat Pandya Sep 22 '12 at 21:24
  • 1
    @JavaWithJava You should probably not have deleted the entire question. – arshajii Sep 22 '12 at 22:12
  • I'm sorry. I didn't want people to waste time trying to solve a problem with a solution. I wasn't thinking about how other people with the same problem can look here. – JavaWithJava Sep 22 '12 at 22:17
  • In that case - you could accept an answer indicating that the problem has been solved. – arshajii Sep 22 '12 at 22:20
  • There will be no point of forum, if the questions being resolved are deleted.. Also, there is no point of answers without a question.. (Other users might think like this in future) if they see this post. – Rohit Jain Sep 22 '12 at 22:26

3 Answers3

3

Try this:

int value = 234567;
String[] digits = Integer.toString(value).split("");
System.out.println(Arrays.toString(digits));

Result:

[, 2, 3, 4, 5, 6, 7]

Take into account that the first (0) position is empty.

Another way using integers:

int value = 234567;
        ArrayList<Integer> result = new ArrayList<Integer>(); 
        while(value > 0){
            result.add(value%10);
            value = value/10;
        }
        Collections.reverse(result);
        System.out.println(result);

Result:

[2, 3, 4, 5, 6, 7]
raven1981
  • 1,415
  • 1
  • 16
  • 20
2

How about this:

int a = 1234321;

char[] c = ("" + a).toCharArray();
int[] digits = new int[c.length];
for (int i = 0 ; i < digits.length ; i++)
    digits[i] = c[i] - '0';

for (int i : digits) System.out.println(i);

Output:

1
2
3
4
3
2
1

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 3
    Why convert an int to a String and then to a char array, inefficiently, and 8 times, when you could do it 0 time using simple modulo arithmetics? – JB Nizet Sep 22 '12 at 21:37
  • just use `System.out.println(Arrays.toString(a));` instead of loop – Bohemian Sep 22 '12 at 21:42
  • 2
    I edited the code so as to not convert the int to a string repetitively. Now it's merely one string conversion and a series of subsequent subtractions. – arshajii Sep 22 '12 at 21:42
-3

create the ArrayList integer type

 ArrayList<Integer> arl = new ArrayList<Integer>();

then insert the values...

arl.add(56);
arl.add(74);
eboix
  • 5,113
  • 1
  • 27
  • 38
Raj Adroit
  • 3,828
  • 5
  • 31
  • 44
  • 3
    I'm not the downvoter, but your answer doesn't compile, and doesn't answer the question. – JB Nizet Sep 22 '12 at 21:29
  • 2
    He's not asking for that.. He is asking to break an integer number into single digits.. 56 into 5 and 6.. And you don't create an ArrayList over primitive types – Rohit Jain Sep 22 '12 at 21:29
  • You can't use generics with primitive data types. You need to use a wrapper class. – eboix Sep 22 '12 at 21:48