Home  >  Article  >  Java  >  Java program to extract the number of a given integer

Java program to extract the number of a given integer

WBOY
WBOYforward
2023-08-29 19:33:031267browse

Java program to extract the number of a given integer

In Java programming, there are some situations where we need to extract a single number from an integer for further manipulation or analysis. This tutorial will guide you on how to extract a number from a given integer using Java.

Syntax

while (num > 0) {
int digit = num % 10;
   System.out.println(digit);
   num = num / 10;
}

The above is the syntax to extract digits from an integer in Java. We keep extracting the last digit by taking the remainder of the number with 10. We divide the number by 10 until it becomes 0.

Extract Digits Using Division and Modulus

A common way to extract numbers from integers is to use division and modulo operations. The idea is to repeatedly divide the number by 10, extract the remainder (representing the rightmost digit), and then update the number by removing the extracted digit.

The Chinese translation of

Example

is:

Example

Let's consider the integer num = 1234. By applying the above algorithm, we can extract each digit and perform desired operations on them

The Chinese translation of

Explanation

is:

Explanation

  • We initialize an integer variable num with the value 1234.

  • Using a while loop, we iterate until the num is greater than 0.

  • In each iteration, we use the modulo operator (%) and 10 to extract the last digit of the current value of num. This operation gives the remainder when num is divided by 10, representing the last digit.

  • The extracted number is stored in the variable digit.

  • We use System.out.println(digit) to display the value of a number, which will print out the number on the console.

  • After extracting the last digit, we update the value of num by dividing it by 10 using the integer division operator (/). This will remove the last digit from num and prepare it for the next iteration.

  • The loop continues until num becomes 0, indicating that all digits have been extracted and displayed.

public class Main {
   public static void main(String args[]) {
      int num = 1234;
      while (num > 0) {
         int digit = num % 10;
         System.out.println(digit);
         num = num / 10;
      }
   }
}

Output

4
3
2
1

Extract numbers using built-in methods

In this scenario, we will extract the individual digits from an integer and display them on the console.

The Chinese translation of

Example 1

is:

Example 1

Let's consider an integer, num = 34512. We will extract and display the digits individually.

The Chinese translation of

Explanation

is:

Explanation

  • We first convert the integer num to a string representation using Integer.toString(num).

  • Next, we iterate through each character in the string using a for loop.

  • For each character, we check if it is a digit using Character.isDigit().

  • If it is a digit, we convert it back to an integer using Character.getNumericValue() and store it in the digit variable.

  • Finally, we use System.out.println() to display each number.

public class Main {
   public static void main(String[] args) {
	int num = 34512;
	String numString = Integer.toString(num);

	for (int i = 0; i < numString.length(); i++) {
	   if (Character.isDigit(numString.charAt(i))) {
	      int digit = Character.getNumericValue(numString.charAt(i));
            System.out.println("Digit: " + digit);
         }
      }
   }
}

Output

Digit: 3
Digit: 4
Digit: 5
Digit: 1
Digit: 2

Example 2

Let us consider another integer, num = 987654. We will extract and display the numbers one by one.

The Chinese translation of

Explanation

is:

Explanation

  • We convert the integer num to a string representation using String.valueOf(num).

  • Instead of using an index-based loop, we utilize an enhanced for loop to iterate over each character in the string.

  • For each character, we check if it is a digit using Character.isDigit().

  • If it is a digit, we convert it back to an integer using Character.getNumericValue() and store it in the digit variable.

  • Finally, we use System.out.println() to display each number.

public class Main {
   public static void main(String[] args) {
	int num = 987654;
	String numString = String.valueOf(num);
	for (char digitChar : numString.toCharArray()) {
	   if (Character.isDigit(digitChar)) {
            int digit = Character.getNumericValue(digitChar);
            System.out.println("Digit: " + digit);
	      }
	   }
   }
}

Output

Digit: 9
Digit: 8
Digit: 7
Digit: 6
Digit: 5
Digit: 4

in conclusion

In this tutorial, we explored the process of extracting a number from a given integer in Java. We covered two different cases: extracting numbers using division and modulo operations and extracting and displaying single numbers.

By applying the methods of division and modulo, we learned how to extract a number by repeatedly dividing it by 10 and using the remainder as the number. This technique allows us to efficiently extract and manipulate integer numbers.

We also explored extracting and displaying the numbers individually. By converting the integer to a string representation, and by iterating over each character, we can check the numbers and display them accordingly. This approach provides flexibility when further processing or analyzing the extracted numbers.

The above is the detailed content of Java program to extract the number of a given integer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete