168,714 views
2 votes
2 votes
Write an application that allows a user to enter the names and birthdates of up to 10 friends. Continue to prompt the user for names and birthdates until the user enters the sentinel value ZZZ for a name or has entered 10 names, whichever comes first. When the user is finished entering names, produce a count of how many names were entered, and then display the names. In a loop, continuously ask the user to type one of the names and display the corresponding birthdate or an error message if the name has not been previously entered. The loop continues until the user enters ZZZ for a name. Save the application as BirthdayReminder.java.

User HolaJan
by
3.2k points

1 Answer

4 votes
4 votes

Answer:

import java.util.Scanner;

public class BirthdayChecker

{

public static void main(String[] args)

{

String sentinelValue = "ZZZ";

final int size = 10;

int count = 0;

String name = null;

String dateOfBirth = null;

String[] namesArray = new String[size];

String[] dateOfBirthsArray = new String[size];

Scanner scanner = new Scanner(System.in);

System.out.println("Enter name or enter ZZZ " + "to quit");

name = scanner.nextLine();

while(!name.equals(sentinelValue) && count < 10)

{

System.out.println("Enter date of birth " + "(dd-mm-yyyy)");

dateOfBirth=scanner.nextLine();

namesArray[count] = name;

dateOfBirthsArray[count] = dateOfBirth;

System.out.println("Enter name or enter " +

"ZZZ to quit");

name=scanner.nextLine();

count++;

}

System.out.println("Count of namesArray = "+count);

System.out.println("The entered namesArray are:");

for(int i = 0; i < count; i++)

{

System.out.println(namesArray[i]);

}

boolean criteria = true;

boolean flag = false;

while(criteria)

{

System.out.println("Enter name to " +

"display date of birth or enter " +

"ZZZ to quit");

name = scanner.nextLine();

if(name.equals(sentinelValue))

criteria = false;

else

{

for(int i = 0; i < count && !flag;

i++)

{

if(namesArray[i].equals(name))

{

flag = true;

dateOfBirth = dateOfBirthsArray[i];

}

}

if(flag)

{

System.out.println("Date of Birth "

+ "of " + name + " is "

+ dateOfBirth);

}

else

{

System.out.println("Date Of Birth "

+ "of " + name + " is not flag");

}

}

flag = false;

}

}

}

Step-by-step explanation:

  • Run a while loop until the name is not ZZZ as well as the count of namesArray is less than 10.
  • Display the information such as the name and count.
  • Ask the user to enter the name and display its date of birth respectively.
  • Check whether the user enters the sentinel value and then assign false to the variable criteria.
  • Go through the namesArray array using a for loop until the flag value is false.
  • Check whether the current name in the namesArray array is equal to the name entered by user, then update the flag variable to true
  • Check whether the value of the variable flag is true and then display the date of birth else display the error message.
User Andy Gherna
by
3.3k points