Beginner Java Programming Please help me so that my user\'s input is of the form
ID: 3865430 • Letter: B
Question
Beginner 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;
}
}
Explanation / Answer
your code is fine just change this method :
private static boolean isValidEmployeeNumber(String employeeNumberStr)
{
if (employeeNumberStr.length() != 12) //1.
{
System.out.println("done1 ");
return false;
}
//Remove this method because id input is in digit so it cant be in Uppercase
/* if (!(Character.isUpperCase(employeeNumberStr.charAt(0)))) //3.
{
System.out.println("done2 ");
return false;
}*/
for (int i = 1; i < employeeNumberStr.length(); i++)
{
if (i == 4 || i == 7) //change this to 4 and 7 because array store from 0 index no, so at index 4 and 7 there is "-"
{
if (employeeNumberStr.charAt(i) != '-') //2.
{
return false;
}
}
else
{
if (!Character.isDigit(employeeNumberStr.charAt(i))) //4.
{
if(i==4||i==7) //Add this to avoid check isDigit at 4 and 7 because at position 4 and 7 there is - which is not a digit
continue;
return false;
}
}
}
return true;
}
for you I'hv added comment but if you have any other problem then just comment and i'll try my best to solve it :)
I hope this solve your error and Dont Forget to give thumbs up :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.