java - How would I put an integer that is storing numbers into an array? -


so have integer z holding values of zip codes , need store each digit of zip code in separate part of array. how able this?

 private int [] zipdigits = new int [5];  private int digitcheck;  private int zipcode;  public zipcode(int z)  { for(int = 0; < 5; i++)     zipdigits[i] =  this.zipcode = z; 

something should work:

public int[] inttodigits(int z) {     int n = string.valueof(z).length();     int[] res = new int[n];     int = n - 1;     while (i >= 0) {         res[i--] = z % 10;         z = z / 10;     }     return res; } 

this first gets length in quite obvious matter, using logarithms don't know if precision lead issues. starts @ of array, takes last digit mod , deletes digit dividing number 10.

edit: see you've edited post state length known 5 digits, don't need calculate length ourselves , can use following algorithm:

public int[] inttodigits(int z) {     int[] res = new int[5];     int = 4;     while (i >= 0) {         res[i--] = z % 10;         z = z / 10;     }     return res; } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -