Question
Java Error error: ';' expected validatePhone(phoneNumber) { ^ Why I am getting the above error? If I add a ; after validatePhone(phoneNumber) I get 8
Java Error
error: ';' expected validatePhone(phoneNumber) { ^
Why I am getting the above error? If I add a ; after validatePhone(phoneNumber) I get 8 different errors.
I'm trying to write this line of code as a method but I'm doing something wrong:
//validate user input and display error message validatePhone(phoneNumber) { return phoneNumber.matches( "\\([1-9]\\)\\d{3}-[1-9]\\d{3}-\\d{4}" )
============================================================================================================================
Entire program
class Sample { public static void main(String[] args) { String phoneNumber; String[] tokens; Boolean validatePhone;
//get user input Scanner scanner = new Scanner (System.in); System.out.println(" Please enter phone number: "); String.phoneNumber = scanner.nextLine();
//validate user input and display error message validatePhone(phoneNumber) { return phoneNumber.matches( "\\([1-9]\\)\\d{3}-[1-9]\\d{3}-\\d{4}" ) } //Validate Phone Number if (validatePhone(phone)){ //process phone number String[]tokens = phoneNumber.split("-|\\(|\\)"); System.out.println("1 "+tokens[1]); System.out.println("2 "+tokens[2]); //tokenized first 3 digits of phone number System.out.println("3 " +tokens[3]);
} else System.out.println("Error!");
} }
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