How can I separate the digits of an int number in Java

0 votes
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,110 points
81,290 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,170 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,900 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,930 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,880 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
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,110 points
1,962 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,900 points
590 views
0 votes
2 answers
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,350 points
289 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,090 points
793 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
297 views
0 votes
2 answers

Integer to String conversion in java

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

answered Jul 27, 2018 in Java by samarth295
• 2,220 points
329 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,900 points
421 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,170 points
311 views
0 votes
2 answers

How can we add leading zeros to the number in Java?

From Java 1.5 you can use the String.format method. ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 59,090 points
2,400 views