1

So if I for example have a large number, say 18750409. I would like to divide this number into separate digits to 1,8,7,5,0,4,0,9. But I don't want to print them out right away, I would like to store them somewhere, like a number A for 1, number B for 8. (In an array possibly?) So I can do more things with these numbers later in the program before printing out results.

Any help is appreciated!

Gabc
  • 89
  • 2
  • 9
  • Do you want to store them as chars or ints (or another number type)? – Java Man Tea Man Feb 21 '16 at 16:12
  • My approach would be to divide it into separate chars, and then convert those chars to number types (not cast!) – Java Man Tea Man Feb 21 '16 at 16:13
  • Would like to store them as ints if possible, but I guess chars would work to – Gabc Feb 21 '16 at 16:13
  • I did look at that first one you linked @Tom but from the answers I saw they printed it out directly. I want to store them and do more operations with these separate numbers – Gabc Feb 21 '16 at 16:15
  • Then just don't add the `System.out.println` line in your code... – Tom Feb 21 '16 at 16:16
  • When you said you had a large number I thought you mean it has 18750409 digits. An `int` can store this number and I would leave it as an `int` for as long as possible and only turn it into digits for displaying purposes. – Peter Lawrey Feb 21 '16 at 17:03

4 Answers4

1

1) Get the length (# of digits) of your number (can be done by converting the number to a string and getting the length by calling .length();

int number = ...;
String my_string = "" + number;
int numberLength = my_string.length();

2) Use a for loop to cycle through your string, and make a separate array of characters and as you go through each char of your string using charAt(), store them in that array:

char[] charArray;
charArray = new char[numberLength];

for(int i = 0; i < (numberLength - 1); i++) // -> Remember, -1 because arrays start at 0!
{
  charArray[i] = my_string.charAt(i);
}

Now you have an array of the digits in char form. To convert them back into ints, just cycle through that array and convert the char back to a number. Note: you can also store the digits as strings.

If you are using chars, to convert to int: use getNumericValue() -> this technically returns unicode numerical value, but unicode of a digit is that digit.

int[] digitArray;
digitArray = new int[numberLength];

for (int i =0; i< numberLength; i++)
{
 digitArray[i] = charArray[i].getNumericValue();
}

And thats it. :) (Sorry if some of the java syntax is off; I have not done java in quite some time.

Java Man Tea Man
  • 242
  • 3
  • 15
0
package test;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author shalom
 */
public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int num,
        temp,     //it holds the remainder value
        n;        //it will be used for seperating the integer
        int[] array;
        String s = "";    // if you want to store the number in string
        System.out.println("Enter the number"); //Taking value from user for any number
        num = new Scanner(System.in).nextInt();

        n = num;

        while (n>0) {
            temp = n%10; //remainder extracted
            s = temp + s;  //concatenated with s
            n = n / 10;    //to get the next digit to be separated

        }

        array = new int[s.length()];    //finally if you want to store the values in array initiliaze the array with lenght of s

        for (int i = 0; i < s.length(); i++)
            array[i] = Integer.parseInt("" + s.charAt(i)); // values are stored in array
    }
}
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
0

You can use ArrayList to store results. And retrieve individual digit i using digitArray.get(i-1)

int n = 18750409;
List<Integer> digitArray = new ArrayList<>();
while(n > 0 ){
  digitArray.add(n%10);
  n = n/10;
}

//to get individual digits
digit3 = digitArray.get(2);
hitz
  • 1,100
  • 8
  • 12
0
  int hjk=326543;
  String cvbn = new String(String.valueOf(hjk));
  System.out.println(cvbn);
  char xcv[] = cvbn.toCharArray();
  int cv[] = new int[xcv.length];
  for(int hj = 0;hj<xcv.length;hj++)
  {
      cv[hj] = Integer.parseInt(String.valueOf(xcv[hj]));
  }

Hope my code helps. Happy coding

SmashCode
  • 741
  • 1
  • 8
  • 14