In: Computer Science
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.
My issue in this is the loop portion. I am not sure if I have done it properly. I also need help trying to comment this code so I can explain it better. Could someone help analyze my code so that I can properly do this JAVA assignment? Please include comments as well inside the code.
Here is what I have so far:
import java.util.*;
public class BirthdayReminder{
public static void main(String[] args)
{
final int NUM_NAMES = 10;
String sentinal = "ZZZ";
int count = 0;
String name = null;
String birthdate = null;
String[] names = new String[NUM_NAMES];
String[] birthdates = new String[NUM_NAMES];
Scanner input = new Scanner(System.in);
System.out.println("Enter a name or " + sentinal + " to quit > ");
name = input.nextLine();
while(name.compareTo(sentinal)!=0 && count < NUM_NAMES)
{
System.out.println("Enter birthdate (mm/dd) > ");
birthdate = input.nextLine();
names[count] = name.trim();
birthdates[count] = birthdate.trim();
System.out.println("Enter a name or " + sentinal + " to quit > ");
name = input.nextLine();
++ count;
}
System.out.println("\nCount of names is " + count);
System.out.println("\nNames are:" + count);
for(int x = 0; x < count; ++x)
System.out.println(names[x]) ;
boolean repeat = true;
boolean found;
while(repeat)
{
found=false;
System.out.println("\nEnter a name or " + sentinal + " to quit > ");
name = input.nextLine().trim();
if(name.compareTo(sentinal)==0)
repeat = false;
else
{
for(int x = 0; x < count; ++x)
{
if(names[x].compareTo(name)==0)
{
found =true;
birthdate = birthdates[x];
break;
}
}
if(found)
System.out.println("Birthdate of " + name + " is " + birthdate);
else
System.out.println("Birthdate of " + name + " is not found");
}
}
}
}
Here is the solution,
The code is working fine. I have just did small addition in the program where after entering all the names, the user then enters any name and associated birthdate gets displayed. It is possible that the user can enter values in small case letters and the name stored in the array is in capital. So to void this i have added the code where both the names i.e from the array and the user entered name gets converted to lowercase and then the comparison take place.
I have added comments for better understanding.
here is the code:-
// your code starts here
import java.util.*;
public class BirthdayReminder{
public static void main(String[] args)
{
// variable declarations
// variable NUM_NAMES for MAX entries
final int NUM_NAMES = 10;
// variable sentinal with value 'ZZZ'
String sentinal = "ZZZ";
// variable to count the names
int count = 0;
// name variable to accept name value
String name = null;
// birthday variable to accept birthdate value
String birthdate = null;
// array for stroring names
String[] names = new String[NUM_NAMES];
// array for storing birthdate
String[] birthdates = new String[NUM_NAMES];
Scanner input = new Scanner(System.in);
// accepting name
System.out.println("Enter a name or " + sentinal + " to quit >
");
name = input.nextLine();
// loop to continue to accept names
// with condition name != 'ZZZ'(to quit) and count<10
// loop breaks if user enter name as 'ZZZ' or count becomes
10
while(name.compareTo(sentinal)!=0 && count <
NUM_NAMES)
{
// accepting birthdate
System.out.println("Enter birthdate (mm/dd) > ");
birthdate = input.nextLine();
//storing value of name in names array
names[count] = name.trim();
// storing value of birthdate in birthdates array
birthdates[count] = birthdate.trim();
// accepting name for next record or 'ZZZ' to quit
System.out.println("Enter a name or " + sentinal + " to quit >
");
name = input.nextLine();
// icrementing count variable to count the names
++ count;
}
// diplaying the count of the names
System.out.println("\nCount of names is " + count);
System.out.println("\nNames are:" + count);
// loop to display names in the array
for(int x = 0; x < count; ++x)
System.out.println(names[x]) ;
// declaring boolean values
// repeat to repeat the while loop till repeat becomes false
boolean repeat = true;
// found variable to be used if the name is found in names
array
boolean found;
while(repeat)
{
// assigning false to found (found is used as a flag value)
found=false;
// accepting name
System.out.println("\nEnter a name or " + sentinal + " to quit >
");
name = input.nextLine().trim();
// comparing with 'ZZZ' to quit
if(name.compareTo(sentinal)==0)
repeat = false;
else
{
// if not 'ZZZ' then iterate through the names array to find the
name
for(int x = 0; x < count; ++x)
{
// if name found
// converting both the values (from array and from the user) to
lowercase
// to avoid uppercase/lowercase problem
if(names[x].toLowerCase().compareTo(name.toLowerCase())==0)
{
// make found = true
found =true;
// get the associated birthdate from birthdates array and break the
loop
birthdate = birthdates[x];
break;
}
}
// outside the for loop
// check if found = true or false
if(found)
// if true print the birthdate
System.out.println("Birthdate of " + name + " is " +
birthdate);
else
// else print appropriate message "not found"
System.out.println("Birthdate of " + name + " is not found");
}
}
}
}
// code ends here
Here is the sample output:-
here is the screenshot of the code:-
Thank You.