How to separate digits of a number in Java

+1 vote
If I have a number 2500, I want the output to be: 2, 5, 0, 0. How can I separate the numbers into digits?
May 3, 2018 in Java by Daisy
• 8,140 points
211,566 views

6 answers to this question.

0 votes

We can easily separate the digits of an integer number using Java.

public static void main(String[] args) {

int num=1020; // int number


while (num > 0) {

    System.out.println( num % 10);

    num = num / 10;

}

}
answered May 3, 2018 by Akrati
• 3,190 points
Sir This One Is showing Reverse Of Input Numbers

Yes @Faizan Ali 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());
}

Hope it works now!!

Thank you!!

0 votes

You can convert a number into String and then you can use toCharArray()  or split() method to separate the number into digits.
 

String number = String.valueOf(someInt);

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

Still unable to separate the digits, enroll now with one of the best Java certification course offered by Edureka.

answered Aug 10, 2018 by Sushmita
• 6,920 points
0 votes
import java.util.*;

public class AmstrongNumber {

public static void main(String args[])

{

int a,i,count=0;

Scanner s= new Scanner(System.in);

System.out.println("Enter a number");

a=s.nextInt();

   while (a > 0) {

i= a % 10;

System.out.println(i);

  a = a / 10;

}

   //System.out.println(a);

}

}
answered Aug 6, 2019 by anonymous
This will print the values in reverse order not the orignal one.
you saved my life bro
0 votes

Hey, Guys 

I have found a new solution to this existing is: 

toCharArray() to Get the Array of Characters. Another way of separating the digits from an int number is using the toCharArray() method. We will convert the integer number into a string and then use the string's toCharArray() to get the characters' array. Now we can print out all the characters one by one.

answered Dec 9, 2020 by Gitika
• 65,770 points
0 votes

Hello guys,

The javaScript split method can be used to split numbers into arrays. But the split method only splits string, so first, you need to convert Number to String. Then you can use the split method with a combination of map method to transform each letter into a Number.

answered Dec 9, 2020 by Rajiv
• 8,870 points
0 votes

You can also have a look here:

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: 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());
}
answered Dec 9, 2020 by Roshni
• 10,480 points

Related Questions In Java

0 votes
1 answer

How to count the number of occurrences of an element in a List?

We can use the static frequency() method. int ...READ MORE

answered Aug 21, 2018 in Java by sharth
• 3,370 points
5,759 views
0 votes
0 answers

How to manage two JRadioButtons in java so that only one of them can be selected at a time?

How to manage two JRadioButtons in java ...READ MORE

Apr 21, 2020 in Java by kartik
• 37,520 points
831 views
0 votes
1 answer

How to round a number to n decimal places in Java?

Use setRoundingMode, set the RoundingMode explicitly to handle your issue ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,770 points

edited Jul 6, 2023 by Khan Sarfaraz 1,898 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,916 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,501 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,620 points
1,298 views
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
1,347 views
0 votes
3 answers

String to Double conversion in java

Double temp = Double.valueOf(str); number = temp.doubleValue(); READ MORE

answered Sep 10, 2018 in Java by Sushmita
• 6,920 points
1,851 views
0 votes
1 answer

How to get the number of digits in an int?

You can find out the length of ...READ MORE

answered May 14, 2018 in Java by Akrati
• 3,190 points
1,046 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,140 points
1,774 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP