In: Computer Science
Write an application, Phone Numbers, that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the output. The phone number has some constraints. Do not let the first three digits contain an 3 or 7 (but do not be more restrictive than that) and ensure that the second set of three digits is not greater than 825. Note that any of the digits can be zero and zeroes should be shown.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. Let me know for any help with any other questions. Thank You! =========================================================================== import java.util.Random; public class PhoneNumber { public static void main(String[] args) { Random random = new Random(); int first3digits[] = {0, 1, 2, 4, 5, 6, 8, 9}; int digit1 = first3digits[random.nextInt(first3digits.length)]; int digit2 = first3digits[random.nextInt(first3digits.length)]; int digit3 = first3digits[random.nextInt(first3digits.length)]; int fourthSixthSeventhDigits = random.nextInt(825); int digit4 = fourthSixthSeventhDigits / 100; int digit5 = (fourthSixthSeventhDigits % 100) / 10; int digit6 = fourthSixthSeventhDigits % 10; int digit7 = random.nextInt(10); int digit8 = random.nextInt(10); int digit9 = random.nextInt(10); int digit10 = random.nextInt(10); System.out.println("Random Phone Number Generated:"); System.out.printf("%d%d%d-%d%d%d-%d%d%d%d\n", digit1,digit2,digit3,digit4,digit5,digit6,digit7,digit8,digit9,digit10); } }
=====================================================================