Question SOLVED: Java Programming

Question SOLVED: Java Programming

Java Programming

 

Please help me so that my user's input is of the form <last name:<year of birth,< employee number

Also please help me with my employee number code, it continues to say invalid.

My Program:

import java.util.Scanner;

public class Assignment5

{

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

String lastName, yearOfBirth, employeeNumber;

System.out.println("Please input a string of the form <last name:<year of birth,< employee number ");

do

{

System.out.print("last name: ");

lastName = in.nextLine();

if(isValidLastName(lastName))

{

System.out.print("valid");

}

else

{

System.out.print("invalid");

}

System.out.println();

  

} while (!isValidLastName(lastName));

  

do

{

System.out.print("birth year: ");

yearOfBirth = in.nextLine();

  

if(isValidYearOfBirth(yearOfBirth))

{

System.out.print("valid");

}

else

{

System.out.print("invalid");

}

System.out.println();

  

} while (!isValidYearOfBirth(yearOfBirth));

  

do

{

System.out.print("employee number: ");

employeeNumber = in.nextLine();

  

if(isValidEmployeeNumber(employeeNumber))

{

System.out.print("valid");

}

else

{

System.out.print("invalid");

}

System.out.println();

  

} while (!isValidEmployeeNumber(employeeNumber));

in.close();   

}

  

//last name method

//1. contains only letters and spaces

//2. begins and ends with a letter

//3. does not contain 2 consecutive spaces

private static boolean isValidLastName(String nameStr)

{

if (!(Character.isLetter(nameStr.charAt(0))) || (!(Character.isLetter(nameStr.charAt(nameStr.length() - 1)))))//2.

{

return false;

}

for (int i = 1; i < nameStr.length() - 1; i++)

{

if (!Character.isLetter(nameStr.charAt(i)))

{

if (nameStr.charAt(i) != ' ') //1.

{

return false;

}

else if (nameStr.charAt(i + 1) == ' ') //3.

{

return false;

}

}

}

return true;

}

  

// birth year method

// integers between 1900 and 1999

// string length = 4

// format: 19DD (D: digit number)

private static boolean isValidYearOfBirth(String yearStr)

{

  

if (yearStr.length() != 4)

{

return false;

}

if (yearStr.charAt(0) != '1' || yearStr.charAt(1) != '9')

{

return false;

}

if (!(Character.isDigit(yearStr.charAt(2)) || Character.isDigit(yearStr.charAt(3))))

{

return false;

}

return true;

}

  

//Employee number method

//LDDD-DD-DDDD

//0123456789

//1. length 11

//2. dash character at char 4 & 7

//3. L: upper case letter

//4. D: digit

private static boolean isValidEmployeeNumber(String employeeNumberStr)

{

if (employeeNumberStr.length() != 12) //1.

{

return false;

}

if (!(Character.isUpperCase(employeeNumberStr.charAt(0)))) //3.

{

return false;

}

for (int i = 1; i < employeeNumberStr.length(); i++)

{

if (i == 5 || i == 8)

{

if (employeeNumberStr.charAt(i) != '-') //2.

{

return false;

  

}

}

else

{

if (!Character.isDigit(employeeNumberStr.charAt(i))) //4.

{

return false;

  

}

}

}

return true;

}

}
Powered by