Question

In this programming assignment we will review string and character manipulations. You will be developing a...

In this programming assignment we will review string and character manipulations. You will be developing a Java program that will prompt users for person’s last name, year of birth, and employee number in a predefined specific format. You will then parse the input string and print it in separate lines along with validations.

The input format will be of the form :,< employee number >

You will need to separate three segments in order to print it as:

Last name

Year of birth

Employee number

Your program will report if these values are Valid, or if they are Invalid.

Functional requirements:

1. A last name is valid if it contains only letters and spaces, it begins and ends with a letter, and it does not contain 2 consecutive spaces.

2. Valid birth years are integers between 1900 and 1999, inclusive, but your program must print Invalid on non-numeric input. Implicitly, this means you must read the data as a string.

3. Valid employee number will have the format LDDD-DD-DDDD, where L is a capital letter and each D is a digit. For example, A991-16-4602.

____________________________________________________________________________________________________________________________________________________

You will need to use at least the indexOf  and substring methods.

Sample Run

Please enter an string as the form of :,< employee number >

Barford:1975,A123-45-6728

last name: Barford

Last Name is Valid

year of birth: 1975

Year of Birth is Valid

license number: A123-45-6728

Employee Number is Valid


Please enter a string as the form of :,< employee number >

Dyer Doan:2001,C123-11-7840

last name: Dyer Doan

Last Name is Valid

year of birth: 2001

Year of Birth is Invalid

employee number: C123-1178-40

Employee Number is Valid


Please enter an string as the form of :,< employee number >

Dy er:98,1135-4498-40

last name: Dy er

Last Name is Invalid

year of birth: 98

Year of Birth is Invalid

employee number: 1135-4498-40

Employee Number is Invalid

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !!

================================================================================================

import java.util.Scanner;

public class Validator {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.print("Please enter an string as the form of :,< employee number >");
       String input = scanner.nextLine();

        String tokens[] = input.split(":");
        String lastName = tokens[0];
        tokens = tokens[1].split(",");
        String yearOfBirth = tokens[0];
        String employeeNumber = tokens[1];

        validateLastName(lastName);
        validateYear(yearOfBirth);
        validateEmployeeNumber(employeeNumber);

    }

    public static void validateLastName(String name) {
        //1. A last name is valid if it contains only letters and spaces,
        // it begins and ends with a letter, and it does not contain 2 consecutive spaces.
        System.out.println("Last Name: " + name);
        for (int i = 0; i < name.length(); i++) {
            char letter = name.charAt(i);
            if (('a' <= letter && letter <= 'z') || ('A' <= letter && letter <= 'Z') || letter == ' ') {

                // valid there are two consecutive spaces below logic validates these conditions
                if (letter == ' ') {
                    if (i == 0) {
                        if (name.charAt(1) == ' ') {
                            System.out.println("Last Name in Invalid");
                            return;
                        }
                    } else if (i == name.length() - 1) {
                        if (name.charAt(i - 1) == ' ') {
                            System.out.println("Last Name in Invalid");
                            return;
                        }
                    }else{
                        if(name.charAt(i-1)==' ' || name.charAt(i+1)==' '){
                            System.out.println("Last Name in Invalid");
                            return;
                        }
                    }
                }
            } else {
                System.out.println("Last Name in Invalid");
                return;
            }
        }

        System.out.println("Last Name is Valid");
    }

    public static void validateYear(String year){

        //2. Valid birth years are integers between 1900 and 1999,
        // inclusive, but your program must print Invalid on non-numeric input.
        // Implicitly, this means you must read the data as a string.
        System.out.println("Year of Birth: "+year);
        try {
            int yearInt = Integer.parseInt(year);
            if(1900<=yearInt && yearInt<=1999){
                System.out.println("Year of Birth is Valid");
            }else{
                System.out.println("Year of Birth is Invalid");
            }

        }catch (NumberFormatException nfe){
            System.out.println("Year of Birth is Invalid");
            return;
        }
    }

    public static void validateEmployeeNumber(String num){

        //3. Valid employee number will have the format LDDD-DD-DDDD,
        // where L is a capital letter and each D is a digit. For example, A991-16-4602.
        System.out.println("License Number: "+num);
        if(num.length()!=12){
            System.out.println("Employee Number is Invalid");
            return;
        }
        if(!('A'<=num.charAt(0) && num.charAt(0)<='Z')){
            System.out.println("Employee Number is Invalid");
            return;
        }

        int firstHypen = num.indexOf('-');
        if(firstHypen==-1){
            System.out.println("Employee Number is Invalid");
            return;
        }
        if(!validateItsNumber(num.substring(1,firstHypen))){
            System.out.println("Employee Number is Invalid");
            return;
        }
        String subString = num.substring(firstHypen+1);
        int secondHypen = subString.indexOf('-');
        if(secondHypen==-1){
            System.out.println("Employee Number is Invalid");
            return;
        }
        if(!validateItsNumber(num.substring(firstHypen+1,firstHypen+1+secondHypen))){
            System.out.println(num.substring(firstHypen,firstHypen+2+secondHypen));
            System.out.println("Employee Number is Invalid");
            return;
        }
        if(!validateItsNumber(subString.substring(secondHypen))){
            System.out.println(subString);
            System.out.println("Employee Number is Invalid");
            return;
        }
        if(!validateItsNumber(subString.substring(secondHypen))){
            System.out.println("Employee Number is Invalid");
            return;
        }

        if (firstHypen!=4 || secondHypen!=2){
            System.out.println("Employee Number is Invalid");
            return;
        }
        System.out.println("Employee Number is Valid");

    }

    public static boolean validateItsNumber(String num){

        try {
            int n= Integer.parseInt(num);
            return true;
        }catch (NumberFormatException e){
            return false;
        }
    }
}

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Add a comment
Know the answer?
Add Answer to:
In this programming assignment we will review string and character manipulations. You will be developing a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write in C Spring 2016 Lab Assignment 11 ET2100 In computer programming in general a "string"...

    Write in C Spring 2016 Lab Assignment 11 ET2100 In computer programming in general a "string" is a sequence of characters. In the C language anything within double quotes is a "string constant" so you have been seeing strings all semester. But we can also have string variables. In the C language these are implemented as an array of char, e.g. char name (10]: In order to make these variables easier to work with, it has been universally agreed that...

  • I need help with this assignment in C++, please! *** The instructions and programming style detai...

    I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...

  • § In BlueJ, create “New Project” named LASTNAME-company Use good programming style and Javadoc documentation standards...

    § In BlueJ, create “New Project” named LASTNAME-company Use good programming style and Javadoc documentation standards Create a “New Class…” named Company to store the employee mappings of employee names (Key) to employee ids (Value) Declare and initialize class constants MIN and MAX to appropriate values Declare and instantiate a Random randomGenerator field object Declare a String field named name for the company’s name Declare a HashMap<String, String> field named employees Add a constructor with only 1 parameter named name...

  • In C++ Please please help.. Assignment 5 - Payroll Version 1.0 In this assignment you must create and use a struct to hold the general employee information for one employee. Ideally, you should use an...

    In C++ Please please help.. Assignment 5 - Payroll Version 1.0 In this assignment you must create and use a struct to hold the general employee information for one employee. Ideally, you should use an array of structs to hold the employee information for all employees. If you choose to declare a separate struct for each employee, I will not deduct any points. However, I strongly recommend that you use an array of structs. Be sure to read through Chapter...

  • Using loops with the String and Character classes. You can also use the StringBuilder class to...

    Using loops with the String and Character classes. You can also use the StringBuilder class to concatenate all the error messages. Floating point literals can be expressed as digits with one decimal point or using scientific notation. 1 The only valid characters are digits (0 through 9) At most one decimal point. At most one occurrence of the letter 'E' At most two positive or negative signs. examples of valid expressions: 3.14159 -2.54 2.453E3 I 66.3E-5 Write a class definition...

  •    Lab/HW 3   Write a program that reads in the following data, all entered on one...

       Lab/HW 3   Write a program that reads in the following data, all entered on one line with at least one space separating them: a) an integer representing a month b) an integer representing the day of the month c) an integer representing a year 1. Check that the month is between 1 and 12. If it’s not print an error message. 2. Day cannot be 0 or less nor can it be more than 31 for all months. Print...

  • JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year...

    JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm/ //made up data 2004/Ali/1212/1219/1 2003/Bob/1123/1222/3 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2001/Jim/1101/1201/3 Text file Class storm{ private String nameStorm; private int yearStorm; private int startStorm; private int endStorm; private int magStorm; public storm(String name, int year, int start, int end, int mag){ nameStorm = name; yearStorm = year; startStorm...

  • Python In this assignment you are asked to enhance the #3 to define a class to...

    Python In this assignment you are asked to enhance the #3 to define a class to model the characteristics of a generic employee. The class is part of a slightly larger program that incl create some employee objects and test its methods. The name of the class is Employee' and includes the following methods and attributes: n program created in Lab udes code to use the class to Method Name Input/ Attributes Output/ Returns Purpose Constructor sets initial values See...

  • Introduction to Java programming You will create a secure password. Ask a user to enter first...

    Introduction to Java programming You will create a secure password. Ask a user to enter first name and last name. (Allow for mixed case) Create a random integer from 10-99. Create the password string that consists of the upper case letter of the last letter of his/her first name, the random number, and the first three letters of his/her last name in lower case. Example someone with the name Rob Lee might have a password that looks like B56lee. Your...

  • programming language in python Solicit string from the keyboard one after another till you get a...

    programming language in python Solicit string from the keyboard one after another till you get a null value input (null means nothing a Carriage Return <CR> hit). During the cycle, determine the length of the key-in. If it is an odd number length (1, 3, 5, ...) then add a colon (":") before concatenating with the next string, otherwise when even number (2, 4, 6, ...) add a dash (") at the end before connecting. Discard (not re-solicit, simply ignore)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT