i am prompting the user to enter an integer through scanner and somehow need to turn that integer into an array (i.e. 12345 = int [] = {1,2,3,4,5}) is there a way to do this? while also ignoring negatives and leading 0s ?
thanks!
<i> </i><script type="text/javascript> function int2arr(val) { var txt = /^(-|0*)/; val = val.replace(txt,''); var num = Math.abs(val * 1).toString(); return num.split(''); } var num = '12345'; alert(int2arr(num).join(',')); // 1,2,3,4,5 num = '-4321'; alert(int2arr(num).join(',')); // 4,3,2,1 num = '01234'; alert(int2arr(num).join(',')); // 1,2,3,4 num = '-0321'; alert(int2arr(num).join(',')); // 3,2,1 </script>
Help @TzManics spread the word by sharing this article on Twitter...