How to get the separate digits of an int number?

2018-12-31 06:06发布

I have numbers like 1100, 1002, 1022 etc. I would like to have the individual digits, for example for the first number 1100 I want to have 1, 1, 0, 0.

How can I get it in Java?

24条回答
姐姐魅力值爆表
2楼-- · 2018-12-31 06:34

I wrote a program that demonstrates how to separate the digits of an integer using a more simple and understandable approach that does not involve arrays, recursions, and all that fancy schmancy. Here is my code:

int year = sc.nextInt(), temp = year, count = 0;

while (temp>0)
{
  count++;
  temp = temp / 10;
}

double num = Math.pow(10, count-1);
int i = (int)num;

for (;i>0;i/=10)
{
  System.out.println(year/i%10);
}

Suppose your input is the integer 123, the resulting output will be as follows:

1
2
3
查看更多
倾城一夜雪
3楼-- · 2018-12-31 06:36

in Java, this is how you can separate digits from numbers and store them in an Array.

public static void main(String[] args) {
        System.out.println("Digits Array:: "+Arrays.toString(getNumberArr(1100)));
}

private static Integer[] getNumberArr(int number) {
    //will get the total number of digits in the number
    int temp = number;
    int counter = 0;

    while (temp > 0) {
        temp /= 10;
        counter++;
    }
    //reset the temp
    temp = number;

    // make an array
    int modulo;     //modulo is equivalent to single digit of the number.
    Integer[] numberArr = new Integer[counter];
    for (int i = counter - 1; i >= 0; i--) {
        modulo = temp % 10;
        numberArr[i] = modulo;  
        temp /= 10;
    }

    return numberArr;
}

Output:

Digits Array:: [1, 1, 0, 0]
查看更多
不流泪的眼
4楼-- · 2018-12-31 06:38

Just to build on the subject, here's how to confirm that the number is a palindromic integer in Java:

public static boolean isPalindrome(int input) {
List<Integer> intArr = new ArrayList();
int procInt = input;

int i = 0;
while(procInt > 0) {
    intArr.add(procInt%10);
    procInt = procInt/10;
    i++;
}

int y = 0;
int tmp = 0;
int count = 0;
for(int j:intArr) {
    if(j == 0 && count == 0) {
    break;
    }

    tmp = j + (tmp*10);
    count++;
}

if(input != tmp)
    return false;

return true;
}

I'm sure I can simplify this algo further. Yet, this is where I am. And it has worked under all of my test cases.

I hope this helps someone.

查看更多
谁念西风独自凉
5楼-- · 2018-12-31 06:39

I haven't seen anybody use this method, but it worked for me and is short and sweet:

int num = 5542;
String number = String.valueOf(num);
for(int i = 0; i < number.length(); i++) {
    int j = Character.digit(number.charAt(i), 10);
    System.out.println("digit: " + j);
}

This will output:

digit: 5
digit: 5
digit: 4
digit: 2
查看更多
几人难应
6楼-- · 2018-12-31 06:39
public int[] getDigitsOfANumber(int number) {
    String numStr = String.valueOf(number);
    int retArr[] = new int[numStr.length()];

    for (int i = 0; i < numStr.length(); i++) {
        char c = numStr.charAt(i);
        int digit = c;
        int zero = (char) '0';
        retArr[i] = digit - zero;

    }
    return retArr;
}
查看更多
泪湿衣
7楼-- · 2018-12-31 06:40

I see all the answer are ugly and not very clean.

I suggest you use a little bit of recursion to solve your problem. This post is very old, but it might be helpful to future coders.

public static void recursion(int number) {
    if(number > 0) {
        recursion(number/10);
        System.out.printf("%d   ", (number%10));
    }
}

Output:

Input: 12345

Output: 1   2   3   4   5 
查看更多
登录 后发表回答