Question

In: Computer Science

Develop a Java application using Parboiled library to write aparser for a customer form. Your...

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

Solutions

Expert Solution

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:


Related Solutions

JAVA 1. Write an application that inputs a telephone number as a string in the form...
JAVA 1. Write an application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed....
Write a program in JAVA using the BigInteger library that can be used to check a...
Write a program in JAVA using the BigInteger library that can be used to check a RSA signature, based on the signer's RSA public key pair. To test your program, take the following information about a message Alice signed and use the verify signature to reproduce the message Alice signed and convert it back to String format. n = 68236588817658935156357212288430888402056854883696767622850112840388111129987 e = 65537 signature = 46612763171375975923246342580942010388414761162366281695045830390867474569531
using Java Your mission in this exercise is to implement a very simple Java painting application....
using Java Your mission in this exercise is to implement a very simple Java painting application. Rapid Prototyping The JFrame is an example that can support the following functions: 1. Draw curves, specified by a mouse drag. 2. Draw filled rectangles or ovals, specified by a mouse drag (don't worry about dynamically drawing the shape during the drag - just draw the final shape indicated). 3. Shape selection (line, rectangle or oval) selected by a combo box OR menu. 4....
USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text...
USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text editor. Use a text field and a button to get the file. Read the entire file as characters and display it in a TextArea. The user will then be able to make changes in the text area. Use a Save button to get the contents of the text area and write that over the text in the original file. Hint: Read each line from...
Write a java application that implements ArrayStack in chapter 16 of your textbook.
Write a java application that implements ArrayStack in chapter 16 of your textbook. Your application should have two files: 1. ArrayStack.java: This file will have the implementation of your stack. 2. TestStack.java: This file will have the main method where you declare your stack object and test the different stack operations. In addition to the ArrayStack methods given in the book, you are to write a toString method. This will allow you to print the Stack object from main. You...
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
Develop a Java application which implements an application for a store chain that has three types...
Develop a Java application which implements an application for a store chain that has three types of stores which are Book, Music, and Movie stores. Your application should have an Item abstract class which should be extended by the Book and Multimedia classes. Item class has abstract priceAfterTax method, you need to implement this method in derived classes. Multimedia class is a superclass for Music and Movie classes. Your project should also include the IPromotion interface, which should be implemented...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name, that does the following: Create two arrays that will hold related information. You can choose any information to store, but here are some examples: an array that holds a person's name and an array that hold's their phone number an array that holds a pet's name and an array that holds what type of animal that pet is an array that holds a student's...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name, that does the following: Declare an array reference variable called myFavoriteSnacks for an array of String type. Create the array so that it is able to hold 10 elements - no more, no less. Fill the array by having each array element contain a string stating one of your favorite foods/snacks. Note: Only write the name of the snack, NO numbers (i.e. Do not...
Develop a Java application that determines the gross pay for each of three employees.
(Salary Calculator) Develop a Java application that determines the gross pay for each of three employees. The company pays straight time for the first 40 hours worked by each employee and time and a half for all hours worked in excess of 40. You’re given a list of the employees, their number of hours worked last week and their hourly rates. Your program should input this information for each employee, then determine and display the employee’s gross pay. Use class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT