Question
Java Help Please: I'm doing something wrong with my while loop. I want my code to check the phone number that is input to make
Java Help Please:
I'm doing something wrong with my while loop. I want my code to check the phone number that is input to make sure it matches. If it doesn't I want it to print out Error! and I want my While loop to ask the user to try again by inputting another phone number. When the user puts in a phone number that matches I want the program to continue to output my tokens. When I run this I get the message that my code is invalid even when I enter a number that matches my pattern. I know it's my while loop because when I comment out the while loop the program recognizes when the number I put in matches. class Main{ public static boolean validatePhone(String phoneNumber){ return phoneNumber.matches( "\\([1-9]\\)\\d{3}-[1-9]\\d{3}-[1-9]\\d{4}" ); } public static void main(String[] args){ String phoneNumber; Boolean validatePhone;
Scanner scanner = new Scanner (System.in); System.out.println(" Please enter phone number: "); phoneNumber = scanner.nextLine();
while(!validatePhone(phoneNumber)){ System.out.println("Error!"); System.out.println(" Enter phone number: "); phoneNumber = scanner.nextLine(); } String[]tokens = phoneNumber.split("-|\\(|\\)"); //String Split Method System.out.printf("1 %s",tokens[1]); //tokenized area code System.out.printf("\2: %s",tokens[2]); //tokenized 1st 3 digits System.out.printf(" 3 : %s",tokens[3]);//tokenized last 4 digits } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started