Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

What is the use of bootstrap program?

Answered: 1 week ago

Question

What is a process and process table?

Answered: 1 week ago

Question

What is Industrial Economics and Theory of Firm?

Answered: 1 week ago

Question

8-6 Who poses the biggest security threat: insiders or outsiders?

Answered: 1 week ago