public class DigitFinder {
/**
* Finds the digit that comes after the target digit when scanning from right to left.
* @param number The number to search within.
* @param target The digit to find.
* @return The digit that comes after the target, or -1 if not found or no next digit.
*/
public static int findNextDigit(int number, int target) {
int next = -1;
while (number > 0) {
int current = number % 10;
number /= 10;
if (current == target) {
return next;
}
next = current;
}
return -1;
}
public static void main(String[] args) {
int number = 8902741;
int target = 2;
int result = findNextDigit(number, target);
if (result != -1) {
System.out.printf("The digit after %d in %d is %d.%n", target, number, result);
} else {
System.out.printf("The digit %d is not found or has no next digit in %d.%n", target, number);
}
}
}
/*
run:
The digit after 2 in 8902741 is 7.
*/