Question

In: Computer Science

Write an application that allows a user to enter the names and birthdates of up to...

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");

}

}

}

}

Solutions

Expert Solution

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.


Related Solutions

C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
Write an application that allows a user to enter any number of student quiz scores, as...
Write an application that allows a user to enter any number of student quiz scores, as integers, until the user enters 99. If the score entered is less than 0 or more than 10, display Score must be between 10 and 0 and do not use the score. After all the scores have been entered, display the number of valid scores entered, the highest score, the lowest score, and the arithmetic average.
Show: Create an application that allows the user to enter the number of calories and fat...
Show: Create an application that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *9 The percentage of...
Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
Write out code for a nested if statement that allows a user to enter in a...
Write out code for a nested if statement that allows a user to enter in a product name, store the product into a variable called product name and checks to see if that product exists in your nested if statement. You must include 5 product names to search for. If it is then assign the price of the item to a variable called amount and then print the product name and the cost of the product to the console. If...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters a number, the program should print out the average of the last 3 numbers (or all numbers if 3 or less have been entered). I would like a detailed explanation so that a beginner level Java programmer could understand.
In Java: Write a nested loop that allows the user to continuously enter phrases and output...
In Java: Write a nested loop that allows the user to continuously enter phrases and output underscores for each character of the phrase. End when the user enters "done" (ignoring case). Once you have this completed, modify your code so that if the character is whitespace, you print a space " " instead of an underscore - Hint: use Character.isWhitespace(YOURCHARHERE) //returns a boolean.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT