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

Something like this will return the char[]:

public static char[] getTheDigits(int value){
    String str = "";
    int number = value;
    int digit = 0;
    while(number>0){
        digit = number%10;
        str = str + digit;
        System.out.println("Digit:" + digit);
        number = number/10;     

    }
    return str.toCharArray();
}
查看更多
像晚风撩人
3楼-- · 2018-12-31 06:41

Here is my answer, I did it for myself and I hope it's simple enough for those who don't want to use the String approach or need a more math-y solution:

public static void reverseNumber2(int number) {

    int residual=0;
    residual=number%10;
    System.out.println(residual);

    while (residual!=number)  {
          number=(number-residual)/10;
          residual=number%10;
          System.out.println(residual);
    }
}

So I just get the units, print them out, substract them from the number, then divide that number by 10 - which is always without any floating stuff, since units are gone, repeat.

查看更多
爱死公子算了
4楼-- · 2018-12-31 06:43

Integer.toString(1100) gives you the integer as a string. Integer.toString(1100).getBytes() to get an array of bytes of the individual digits.

Edit:

You can convert the character digits into numeric digits, thus:

  String string = Integer.toString(1234);
  int[] digits = new int[string.length()];

  for(int i = 0; i<string.length(); ++i){
    digits[i] = Integer.parseInt(string.substring(i, i+1));
  }
  System.out.println("digits:" + Arrays.toString(digits));
查看更多
不再属于我。
5楼-- · 2018-12-31 06:43
import java.util.Scanner;

public class SeparatingDigits {

    public static void main( String[] args )
    {

        System.out.print( "Enter the digit to print separately :- ");
        Scanner scan = new Scanner( System.in );

        int element1 = scan.nextInt();
        int divider;

        if( ( element1 > 9999 ) && ( element1 <= 99999 ) )
        {
            divider = 10000;
        }
        else if( ( element1 > 999 ) && ( element1 <= 9999 ) )
        {
            divider = 1000;
        }
        else if ( ( element1 > 99) && ( element1 <= 999 ) )
        {
            divider = 100;
        }
        else if( ( element1 > 9 ) && ( element1 <= 99 ) )
        {
            divider = 10;
        }
        else 
        {
            divider = 1;
        }

        quotientFinder( element1, divider );




    }

     public static void quotientFinder( int elementValue, int dividerValue )
     {
         for( int count = 1;  dividerValue != 0; count++)
         {
            int quotientValue = elementValue / dividerValue ;
            elementValue = elementValue % dividerValue ;
            System.out.printf( "%d  ", quotientValue );

            dividerValue /= 10;

         }
     }
    }

Without using arrays and Strings . ( digits 1-99999 )

output :

Enter the digit to print separately :- 12345

1 2 3 4 5

查看更多
牵手、夕阳
6楼-- · 2018-12-31 06:44

Convert it to String and use String#toCharArray() or String#split().

String number = String.valueOf(someInt);

char[] digits1 = number.toCharArray();
// or:
String[] digits2 = number.split("(?<=.)");

In case you're already on Java 8 and you happen to want to do some aggregate operations on it afterwards, consider using String#chars() to get an IntStream out of it.

IntStream chars = number.chars();
查看更多
琉璃瓶的回忆
7楼-- · 2018-12-31 06:47

Java 8 solution to get digits as int[] from an integer that you have as a String:

int[] digits = intAsString.chars().map(i -> i - '0').toArray();
查看更多
登录 后发表回答