Friday, 15 June 2012

java - Simple Number to Array with Individual Digits -



java - Simple Number to Array with Individual Digits -

i exceptionally new programming, working on improving skills programmer. currently, working on problem gave myself trying take variable number , create each of digits separate number in array. don't care order of digits, if reversed, doesn't matter me. know people have asked question numerous times, seem utilize lot of things don't understand. since school doesn't offer java courses, know have learned on own, if explain terms utilize in code aren't extremely trivial, wonderful. right now, have written:

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

this works fine printing digits individually, can't figure out how add together them array. appreciate help can give, , please maintain in mind much prefer simplicity on little size. give thanks in advance!

p.s. responses i've seen similar questions include think arrays of strings. in order part of programme have working still work, think need utilize array of integers. if you're curious, rest of code used determine if numbers in array different, in order accomplish final result of determining if number's digits distinct. looks this:

int repeats=0; int[] digitarray; digitarray = new int[10]; (int = 0; < digitarray.length; i++) (int j = 0; j < digitarray.length; j++) if ((i != j) && (digitarray[i]==digitarray[j])) unique = unique+1; system.out.println(unique==0);

method number.tostring().length() homecoming number of digits. same length of needed array. utilize code before, yet instead of printing add together digit array.

int number = 1234567890; int len = integer.tostring(number).length(); int[] iarray = new int[len]; (int index = 0; index < len; index++) { iarray[index] = number % 10; number /= 10; }

java arrays digits

No comments:

Post a Comment