// This program is an example of pulling a number apart digit by digit // and also contains the general form for pulling a String apart // character by character public class ContainsDigit { public static void main(String[] args) { // Example pulling string apart: String s = "hello"; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); // Now, we can work with the specific character, c } System.out.println("contains7 for 45 : " + contains7(45)); System.out.println("contains7 for 879 : " + contains7(879)); } // Returns whether a number has a 7 digit in it anywhere public static boolean contains7(int num) { // num = Math.abs(num); --> only needed if num can be < 0 // while (digit != 7) { --> a natural test that actually turns out to be hard to create a solution with // keep going while 'num' still has digits left to look through while (num > 0) { // num != 0 works too int digit = num % 10; // the digit from the ones place // Now, we can work with the specific digit // work with that digit. For this problem, we want to know if we found a 7 if (digit == 7) { // early return : we can stop looking if we find a 7 -- return true and bail out of the method return true; } num = num / 10; // the rest of the digits, shifted to the right by one place } // however, we have to wait until after the while loop to know that the number contains no 7s anywhere return false; } }