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:27

How about this?

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

or instead of printing to the console, we can collect it in an array of integers and then print the array:

public static void main(String[] args) {
    Integer[] digits = getDigits(12345);
    System.out.println(Arrays.toString(digits));
}

public static Integer[] getDigits(int num) {
    List<Integer> digits = new ArrayList<Integer>();
    collectDigits(num, digits);
    return digits.toArray(new Integer[]{});
}

private static void collectDigits(int num, List<Integer> digits) {
    if(num / 10 > 0) {
        collectDigits(num / 10, digits);
    }
    digits.add(num % 10);
}
查看更多
素衣白纱
3楼-- · 2018-12-31 06:29

Since I don't see a method on this question which uses Java 8, I'll throw this in. Assuming that you're starting with a String and want to get a List<Integer>, then you can stream the elements like so.

List<Integer> digits = digitsInString.chars().map(Character::getNumericValue)
                            .collect(Collectors.toList());

This gets the characters in the String as a IntStream, maps those integer representations of characters to a numeric value, and then collects them into a list.

查看更多
一个人的天荒地老
4楼-- · 2018-12-31 06:29

I think this will be the most useful way to get digits:

public int[] getDigitsOf(int num)
{        
    int digitCount = Integer.toString(num).length();

    if (num < 0) 
        digitCount--;           

    int[] result = new int[digitCount];

    while (digitCount-- >0) {
        result[digitCount] = num % 10;
        num /= 10;
    }        
    return result;
}

Then you can get digits in a simple way:

int number = 12345;
int[] digits = getDigitsOf(number);

for (int i = 0; i < digits.length; i++) {
    System.out.println(digits[i]);
}

or more simply:

int number = 12345;
for (int i = 0; i < getDigitsOf(number).length; i++) {
    System.out.println(  getDigitsOf(number)[i]  );
}

Notice the last method calls getDigitsOf method too much time. So it will be slower. You should create an int array and then call the getDigitsOf method once, just like in second code block.

In the following code, you can reverse to process. This code puts all digits together to make the number:

public int digitsToInt(int[] digits)
{
    int digitCount = digits.length;
    int result = 0;

    for (int i = 0; i < digitCount; i++) {
        result = result * 10;
        result += digits[i];
    }

    return result;
}

Both methods I have provided works for negative numbers too.

查看更多
浅入江南
5楼-- · 2018-12-31 06:30

I noticed that there are few example of using Java 8 stream to solve your problem but I think that this is the simplest one:

int[] intTab = String.valueOf(number).chars().map(Character::getNumericValue).toArray();

To be clear: You use String.valueOf(number) to convert int to String, then chars() method to get an IntStream (each char from your string is now an Ascii number), then you need to run map() method to get a numeric values of the Ascii number. At the end you use toArray() method to change your stream into an int[] array.

查看更多
泛滥B
6楼-- · 2018-12-31 06:30
int number = 12344444; // or it Could be any valid number

int temp = 0;
int divider = 1;

for(int i =1; i< String.valueOf(number).length();i++)
 {

    divider = divider * 10;

}

while (divider >0) {

    temp = number / divider;
    number = number % divider;
    System.out.print(temp +" ");
    divider = divider/10;
}
查看更多
妖精总统
7楼-- · 2018-12-31 06:31
// could be any num this is a randomly generated one
int num = (int) (Math.random() * 1000);

// this will return each number to a int variable
int num1 = num % 10;
int num2 = num / 10 % 10;
int num3 = num /100 % 10;

// you could continue this pattern for 4,5,6 digit numbers
// dont need to print you could then use the new int values man other ways
System.out.print(num1);
System.out.print("\n" + num2);
System.out.print("\n" + num3);
查看更多
登录 后发表回答