java digit in number

www.la‮iruttu‬.com
java digit in number

To check if a number contains a specific digit in Java, you can use the String.contains() method to check if the string representation of the number contains the digit as a substring.

Here's an example of how to do this:

int number = 12345;
boolean contains3 = String.valueOf(number).contains("3");
System.out.println("Number contains digit 3: " + contains3);  // Outputs: Number contains digit 3: true

This will output true because the number 12345 contains the digit 3.

Alternatively, you can use the Character.isDigit() method to check if a specific character is a digit, and iterate over the characters in the string representation of the number to see if any of them are the digit you're looking for.

Here's an example of how to do this:

int number = 12345;
char digit = '3';
boolean contains3 = false;
for (char c : String.valueOf(number).toCharArray()) {
    if (Character.isDigit(c) && c == digit) {
        contains3 = true;
        break;
    }
}
System.out.println("Number contains digit 3: " + contains3);  // Outputs: Number contains digit 3: true

This will also output true because the number 12345 contains the digit 3.

Created Time:2017-11-03 00:14:54  Author:lautturi