How to get the separate digits of an int number?

前端 未结 30 1967
陌清茗
陌清茗 2020-11-22 03:03

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

相关标签:
30条回答
  • 2020-11-22 03:30

    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)
            .boxed()
            .collect(Collectors.toList());
    

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

    0 讨论(0)
  • 2020-11-22 03:31

    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]
    
    0 讨论(0)
  • 2020-11-22 03:32

    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));
    
    0 讨论(0)
  • 2020-11-22 03:33

    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
    
    0 讨论(0)
  • 2020-11-22 03:34

    To do this, you will use the % (mod) operator.

    int number; // = some int
    
    while (number > 0) {
        print( number % 10);
        number = number / 10;
    }
    

    The mod operator will give you the remainder of doing int division on a number.

    So,

    10012 % 10 = 2
    

    Because:

    10012 / 10 = 1001, remainder 2
    

    Note: As Paul noted, this will give you the numbers in reverse order. You will need to push them onto a stack and pop them off in reverse order.

    Code to print the numbers in the correct order:

    int number; // = and int
    LinkedList<Integer> stack = new LinkedList<Integer>();
    while (number > 0) {
        stack.push( number % 10 );
        number = number / 10;
    }
    
    while (!stack.isEmpty()) {
        print(stack.pop());
    }
    
    0 讨论(0)
  • 2020-11-22 03:34

    This uses the modulo 10 method to figure out each digit in a number greater than 0, then this will reverse the order of the array. This is assuming you are not using "0" as a starting digit.

    This is modified to take in user input. This array is originally inserted backwards, so I had to use the Collections.reverse() call to put it back into the user's order.

        Scanner scanNumber = new Scanner(System.in);
        int userNum = scanNumber.nextInt(); // user's number
    
        // divides each digit into its own element within an array
        List<Integer> checkUserNum = new ArrayList<Integer>();
        while(userNum > 0) {
            checkUserNum.add(userNum % 10);
            userNum /= 10;
        }
    
        Collections.reverse(checkUserNum); // reverses the order of the array
    
        System.out.print(checkUserNum);
    
    0 讨论(0)
提交回复
热议问题