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

In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java application. Given a set of events, choose the resulting programming actions. Understand the principles behind Java. Understand the basic principles of object-oriented programming including classes and inheritance. Deliverables .java files as requested below. Requirements Create all the panels. Create the navigation between them. Start navigation via the intro screen. The user makes a confirmation to enter the main panel. The user goes to the...
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
NEED IN JAVA Problem statement. This application will support the operations of a technical library for...
NEED IN JAVA Problem statement. This application will support the operations of a technical library for an R&D organization. This includes the searching for and lending of technical library materials, including books, videos, and technical journals. Users will enter their company ids in order to use the system; and they will enter material ID numbers when checking out and returning items.   Each borrower can be lent up to five items. Each type of library item can be lent for a...
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 java Define a class Library based on the following specifications: a. A library can have...
using java Define a class Library based on the following specifications: a. A library can have multiple books. Decide on the best possible data structure to store books. b. A library has a name. c. A library has an address. Define a 2-argument constructor for the Library class. Decide on the arguments needed for this constructor. e. Define a method that can add new books to the library. f. Define a method that allows a book to be borrowed (checked...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT