Question: Beginner Java Programming- please Help, thank you. Program Directions: You will be developing a Java program that will prompt users for persons last name, year
Beginner Java Programming- please Help, thank you.
Program Directions:
You will be developing a Java program that will prompt users for persons 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.
You will need to use at least the indexOf and substring methods.
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.
Example:
Please enter a 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
Employee Number: A123-45-6728
Employee Number is Valid
My Program so far: (I don't have any indexOf or substring, my input method isn't correct and I cant get the right output )
import java.util.Scanner;
public class Assignment05
{
public static void main(String args[])
{
String lastName, birthYear, employeeNumber;
Scanner in = new Scanner(System.in);
System.out.println("Please enter an string as the form of :,< employee number > :");
do
{
System.out.print("Enter a last name: ");
lastName = in.nextLine();
}while(!correctName(lastName));
do
{
System.out.print("Enter year of birth: ");
birthYear = in.nextLine();
}while(!correctYear(birthYear));
do
{
System.out.print("Enter employee number: ");
employeeNumber = in.next();
}while(correctEmployeeNumber(employeeNumber));
if(isValid( lastName, birthYear, employeeNumber))
{
System.out.println("Valid");
}
else System.out.println("Invalid");
}
//last name method
public static boolean correctName(String str)
{
if((str.charAt(0) >= 'a' && str.charAt(str.length() - 1) < 'z') || (str.charAt(str.length() - 1) >= 'A' && str.charAt(str.length() - 1) <= 'Z'))
{
}
else
{
return false;
}
for(int count = 0; count < str.length(); count++)
{
if(count > 0 && str.charAt(count) == ' ' && str.charAt(count - 1) == ' ')
{
return false;
}
if((str.charAt(count) >= 'a' && str.charAt(count) <= 'z') || (str.charAt(count) >= 'A' && str.charAt(count) <= 'Z') || str.charAt(count) == ' ')
{
}
else
{
return false;
}
}
return true;
}
//birth year method
public static boolean correctYear(String str2)
{
if(str2.length() != 4)
{
return false;
}
if(str2.charAt(0) == '1' && str2.charAt(1) == '9' && (str2.charAt(2) >= '0' && str2.charAt(2) <= '9') && (str2.charAt(3) >= '0' && str2.charAt(3) <= '9'))
{
return true;
}
else
{
return false;
}
}
//employee number method
public static boolean correctEmployeeNumber(String str3)
{
if(str3.length() != 12)
{
return false;
}
if(str3.charAt(0) >= 'A' && str3.charAt(0) <= 'Z')
{
}
else
{
return false;
}
for(int i = 1; i < str3.length(); i++)
{
if((i == 4 || i == 7 ) && i != '-')
{
return false;
}
else
{
if(i <= '0' || i >= '9')
{
return false;
}
}
}
return true;
}
//Validity method
public static boolean isValid(String a, String l, String b)
{
if((a.charAt(0) + "").equalsIgnoreCase(l.charAt(0) + ""))
{
}
else
{
return false;
}
if(a.charAt(8) != b.charAt(b.length() - 2))
{
return false;
}
if(a.charAt(10) != b.charAt(b.length() - 1))
{
return false;
}
return true;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
