Your Task For This Project Is To Write A Parser

Question

Your task for this project is to write a parser for a customer form. You need to develop a Java a…

  

Your task for this project is to write a parser for a customer form. You need to develop a Java application using Parboiled library. Your program should include a grammar for the parser and display the parse tree with no errors. Remember that your fifth and sixth assignments are very similar to this project and you can benefit from them. The customer form should include the following structure: First name, middle name (optional), last name Street address, city, state (or province), country Phone number Rules First name, middle name and last name should start with an uppercase letter followed by lower . The street address can include any type of letters or numbers. There is no lowercase/uppercase The city name should start with an uppercase letter followed by lowercase/uppercase letters. case letters. Middle name is an optional input meaning such an input may or may not exist. restriction either The state or province should start with an uppercase letter followed by lowercase/uppercase letters The country name should start with an uppercase letter followed by lowercase/uppercase letters The phone number should begin with area code in parenthesis, followed by a space, three digits, . .
a hyphen, and four digits. Examples Here are some input examples for your program: John Doe 3609 Mynders Ave, Memphis, TN, United States of America (901) 345-9834 Joshua R. Clemens 982 Pryor St, Little Rock, AR, USA (501) 876-1234 Mary Smith 82 Main St., Toronto, Ontario, Canada (416) 509-6995 Please do everything on your own and submit your project in a zip file. Your zip folder should include your project along with its source files, screenshots of sample input, and screenshots of outputs

Solutions

Expert Solution

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE
TO HELP YOU

RATE THUMBSUP PLEASE

ANSWER:

CODE:

import java.util.*;

class Validation{

public static void main(String
args[]){

Scanner in = new
Scanner(System.in);

String name, fname, lname, mname, address,
st_address, city, state, country, phone;

while(true){

System.out.print("Enter name: ");

name = in.nextLine();

StringTokenizer st = new StringTokenizer(name, "
");

if(st.countTokens()<2)

System.out.println("Invalid name, must be Firstname
Middlename(optional) Lastname");

else{

boolean valid = true;

while(st.hasMoreTokens()){

if(!isValidName(st.nextToken())){

System.out.println("Invalid name, Firstname,
Middlename, Lastname should start with an uppercase letter followed
by lower case letters");

valid = false;

}

}

if(valid)

break;

}

}

while(true){

System.out.print("Enter address:
");

address = in.nextLine();

StringTokenizer st = new StringTokenizer(address,
",");

if(st.countTokens()<4)

System.out.println("Invalid name, must be Firstname
Middlename(optional) Lastname");

else{

boolean valid = true;

int i=0;

while(st.hasMoreTokens()){

if(i==0){

st.nextToken();

i++;

continue;

}

if(!isValidAddress(st.nextToken())){

System.out.println("Invalid address, Street address,
city, state (or province), country should start with an uppercase
letter followed by lower case letters");

valid = false;

break;

}

}

if(valid)

break;

}

}

while(true){

System.out.print("Enter Phone number:
");

phone = in.nextLine();

if(isValidPhone(phone))

break;

System.out.println("Invalid Phone number, should
begin with area code in parenthesis, followed by a space, three
digits, a hyphen, and four digits.");

}

System.out.println("\nEntered Details are:
");

System.out.println(name+"\n"+address+"\n"+phone);

}

public static boolean isValidName(String
str){

if(str.length()==0)

return false;

if(str.charAt(0)<'A' ||
str.charAt(0)>'Z')

return false;

for(int i=1; i<str.length();
i++){

if((str.charAt(i)<'a' || str.charAt(i)>'z')
&& str.charAt(i)!=' ' &&
str.charAt(i)!='.')

return false;

}

return true;

}

public static boolean isValidAddress(String
str){

str = str.trim();

if(str.length()==0)

return false;

if(str.charAt(0)<'A' ||
str.charAt(0)>'Z')

return false;

for(int i=1; i<str.length();
i++){

if((str.charAt(i)<'a' || str.charAt(i)>'z')
&& (str.charAt(i)<'A' || str.charAt(i)>'Z')
&& str.charAt(i)!=' ' &&
str.charAt(i)!='.')

return false;

}

return true;

}

public static boolean isValidPhone(String
mobile){

if(mobile.length()!=14)

return false;

if(mobile.charAt(0)=='(' &&
mobile.charAt(4)==')' && mobile.charAt(5)==' ' &&
mobile.charAt(9)=='-'){

if(Integer.parseInt(mobile.substring(1, 3))!=0
&& Integer.parseInt(mobile.substring(6, 8))!=0 &&
Integer.parseInt(mobile.substring(10, 13))!=0)

return true;

}

return false;

}

}

RATE THUMBSUP PLEASE

THANKS


Submit Your Answer

[contact-form-7 404 "Not Found"]