How to get the separate digits of an int number?

前端 未结 30 1965
陌清茗
陌清茗 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:18

    As a noob, my answer would be:

    String number = String.valueOf(ScannerObjectName.nextInt()); 
    int[] digits = new int[number.length()]; 
    for (int i = 0 ; i < number.length() ; i++)
        int[i] = Integer.parseInt(digits.substring(i,i+1))
    

    Now all the digits are contained in the "digits" array.

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

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

    Why don't you do:

    String number = String.valueOf(input);
    char[] digits = number.toCharArray();
    
    0 讨论(0)
  • 2020-11-22 03:23
    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;
    }
    
    0 讨论(0)
  • 2020-11-22 03:23
    import java.util.Scanner;
    
    class  Test 
    {  
        public static void main(String[] args)   
        {  
            Scanner sc = new Scanner(System.in); 
    
    
        int num=sc.nextInt(); 
        System.out.println("Enter a number (-1 to end):"+num);
        int result=0;
        int i=0;
        while(true) 
        { 
          int n=num%10;
          if(n==-1){
            break;
          }
          i++;
          System.out.println("Digit"+i+" = "+n);
          result=result*10+n;
          num=num/10; 
    
    
          if(num==0) 
          { 
            break; 
          } 
        }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:24

    Try this:

    int num= 4321
    int first  =  num % 10;
    int second =  ( num - first ) % 100 / 10;
    int third  =  ( num - first - second ) % 1000 / 100;
    int fourth =  ( num - first - second - third ) % 10000 / 1000;
    

    You will get first = 1, second = 2, third = 3 and fourth = 4 ....

    0 讨论(0)
提交回复
热议问题