In: Computer Science
Develop a Java application using Parboiled library to write a parser for a customer form. Your program should include a grammar for the parser and display the parse tree with no errors.
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 case letters. Middle name is an optional input meaning such an input may or may not exist. • The street address can include any type of letters or numbers. There is no lowercase/uppercase restriction either.
• The city name should start with an uppercase letter followed by lowercase/uppercase letters.
• 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.
Example:
John Doe
3609 Mynders Ave, Memphis, TN, United States of America
(901) 345-9834
Solution:
code:
package chegg;
import java.util.*;
public class ValidatingTheGrammar{
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 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 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; } } Output: