7

I am exceptionally new to programming, but I am working on improving my skills as a programmer. Currently, I am working on a problem I gave myself where I am trying to take a variable number and make each of its digits a separate number in an array. I don't care about the order of the digits, so if they are reversed, then it doesn't matter to me. I know people have asked this question numerous times, but they always seem to use a lot of things that I don't understand. Since my school doesn't offer any Java courses, I only know what I have learned on my own, so if you could explain any terms you use in the code that aren't extremely trivial, that would be wonderful. Right now, I have written:

int number = 1234567890;
    while (number > 0) {
        System.out.println(number%10);
        number = number/10; 

This works fine for printing the digits individually, but I can't figure out how to add them to the array. I greatly appreciate any help you can give, and please keep in mind that I much prefer simplicity over small size. Thank you in advance!

P.S. Some responses I've seen for similar questions include what I think are arrays of strings. In order for the part of the program that I have working to still work, I think that I need to use an array of integers. If you're curious, the rest of the code is used to determine if the numbers in the array are all different, in order to achieve the final result of determining if a number's digits are all distinct. It looks like this:

int repeats=0;
int[] digitArray;
digitArray = new int[10];
for (int i = 0; i < digitArray.length; i++) 
    for (int j = 0; j < digitArray.length; j++)
        if ((i != j) && (digitArray[i]==digitArray[j])) unique = unique+1;
System.out.println(unique==0);
  • In this case, we don't know size ahead of time, so I think arraylist would be good here. If you use arraylist, all you need to do would be yourList.add(number%10); because of autoboxing, java5 onwards it will work. – kosa Feb 07 '13 at 03:48
  • I don't really know what an arraylist is... Is it like an array, but with a different name and a dynamic size? –  Feb 07 '13 at 03:56
  • 1
    Yes, it is dynamic array which is backed by array.http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html – kosa Feb 07 '13 at 03:58
  • @MatthewTyler.. Exactly. I have added a link to the documentation in the first line of my answer. You can check that. – Rohit Jain Feb 07 '13 at 03:58
  • This is really an awesome place! I'll look into that list array. It looks like it could not only help me solve this problem, but could also be quite helpful in the future. Thank you so much for all of your help. –  Feb 07 '13 at 04:01
  • Integer.toString(n).chars().map(a->a-'0').toArray(); – David S Alderson Jan 02 '17 at 05:44

4 Answers4

9

Method number.toString().length() will return the number of digits. That is the same as the length of your needed array. Then you use your code as before, yet instead of printing you add the digit to the array.

int number = 1234567890;
int len = Integer.toString(number).length();
int[] iarray = new int[len];
for (int index = 0; index < len; index++) {
    iarray[index] = number % 10;
    number /= 10;
}
Insomniac631
  • 1,018
  • 12
  • 20
Max Dietz
  • 178
  • 5
  • Wow! That was a quick reply. That is really helpful, too. My only question is, Why are you using "Integer" instead of "int", which is what I usually use? –  Feb 07 '13 at 03:57
  • 1
    @MatthewTyler.. You can work with an `int` too. `Integer` is not necessarily required here. In fact, you should use `int` wherever possible. And only use `Integer`, where you can't manage without it, e.g. in a generic `ArrayList` declaration, where you can't have - `ArrayList`, but only `ArrayList` – Rohit Jain Feb 07 '13 at 03:59
  • @RohitJain.. I see. I've never heard of using Integer before, so I think I'll stick with int whenever possible. –  Feb 07 '13 at 04:03
  • I use Integer because int does not have a toString() method meaning you would have to use an ArrayList instead of a simple array. – Max Dietz Feb 07 '13 at 14:33
  • @MaxDietz What this have to do with the `toString` method in Integer? And how will it affect the use of `int[]` instead of `Integer[]`? – Rohit Jain Feb 08 '13 at 04:46
  • Edited to make a bit clearer. When initializing an array, which is a much simpler data structure than an ArrayList, you need to know its length beforehand, which is equal to the number of digits. To do so, casting to a string and counting the characters is the same as knowing the digits. Integer is an object that encapsulates an int, basically it stores an int but also has some methods, like toString, equals, etc.. It has a bit of bloat, and can be replaced by my edit, namely the static method `Integer.toString` as shown. – Max Dietz Feb 08 '13 at 14:40
5

I would rather suggest you to use an ArrayList, since to use an array, you would have to allocate the size in advance, for which you need to know the number of digits in your number, which you don't know.

So, either work with an array, and do the iteration over the number twice - once for finding size, and next for doing actual work. Else, move ahead with an ArrayList.

Adding an element to an ArrayList is quite simple. You just need to call - List#add(E) method with appropriate parameter.

So, here's an extension of your solution: -

// Declare a List<Integer>, since it will store integers only.
List<Integer> digits = new ArrayList<Integer>():

int number = 1234567890;
while (number > 0) {
    int digit = number % 10;  // Store digit in a variable
    number = number/10;

    // Add digit to the list
    digits.add(digit);
}

Alternatively, if you want to have only unique digits in your List, then you should use a HashSet, which automatically removes the duplicates.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Thanks for the helpful and speedy reply! Do you think you could explain the HashSet a bit more? That function intrigues me, because it seems like it could potentially make my code much simpler. –  Feb 07 '13 at 03:59
  • 1
    See the documentation - http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html . A HashSet is nothing but a `Set`, that stores just unique element. You know `sets` in mathematics right? It's just that. You can get more details in docs. – Rohit Jain Feb 07 '13 at 04:01
  • I greatly appreciate all of the help you have thus far given, but if you could show me how I would implement the HashSet function into the actual code, then that would be absolutely superb. Thank you for your time. –  Feb 07 '13 at 04:04
  • @MatthewTyler.. Just replace the declaration of `ArrayList` with a `HashSet`. It goes like this: - `Set digits = new HashSet();`. And the rest is same. – Rohit Jain Feb 07 '13 at 04:16
  • I get it now. Thanks for the information. I'll try that. Do you know why it comes up with an error whenever number is too large? My current code is as follows: –  Feb 07 '13 at 04:18
  • 1
    @MatthewTyler.. Because you can't store too large number in an `int`. Try using `long` type for your original number. – Rohit Jain Feb 07 '13 at 04:23
  • Oh, I see. Thanks. I'm getting a warning to not use extended discussion in comments, so I'll stop bugging you now. You have been a tremendous help. Thank you, and have a wonderful day. –  Feb 07 '13 at 04:27
  • @MatthewTyler.. Did you have any issue with this solution? – Rohit Jain Feb 08 '13 at 04:48
0

With Java 8:

Integer.toString(n).chars().map(a->a-'0').toArray()

0
char [] arr = scan.next().toCharArray();

This code will read a number from scan.next() and then it is going to give it as an input to char array which will have the number at its indices as single digit by digit.

Hope this will help.

roottraveller
  • 7,942
  • 7
  • 60
  • 65
akay
  • 91
  • 1
  • 10