+1 (315) 557-6473 

Console Records Assignment Solution using Java


Console Records Assignments

You have to write a Java assignment program that accomplishes the following:

When the program is run, prompt the user in the console to enter records made up of a name and age in two different prompts. After a user enters a name, then prompt them for age. After each name/age record has been entered, prompt the user to enter an additional record by selecting "yes/no". After a user has entered a name and then an age. If the user selects "yes," prompt to enter a name and age again. There should NOT be any limit to the number of records a user can enter. When the user has finished entering their records (by selecting "no" at the enter another record prompt), display the records entered in the console. Now prompt the user whether they would like to write the records to a file with the message: "Would you like to write to a file? yes/no". If the user responds "yes," prompt the user for the filename they would like to use. Once the filename is entered, create the file and write the records to that file, with each record appearing on a separate line. If the user responds "no" at the write to a file prompt, then display the message: "Thanks for playing!"

Solution:

import java.io.BufferedWriter; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class homeWork3 { public static void main(String[] args) throws Exception { // Create Scanner object Scanner sc = new Scanner(System.in); // Create array to store the names entered List names = new ArrayList(); // Create a List to store all the ages entered List ages = new ArrayList(); // The two lists declared above are parallel, which means that the name at index i corresponds to the age at index i /* Begin with main program here. We will create an infinite-loop that will be executed until the user enters the option "no" */ boolean program_running = true; while(program_running) { // First, ask user for name and store in a variable called 'name' String name; System.out.print("Enter name: "); name = sc.nextLine(); names.add(name); // Now, ask user for age int age; age = getAge(sc); ages.add(age); // Ask if he/she wants to enter more names and ages System.out.print("Do you want to enter a new record? "); String input = askYesNo(sc); if(input.compareTo("no") == 0) // user enters no program_running = false; // break the while-loop } // Display records System.out.println(""); System.out.println(String.format("%-10s %10s", "Name", "Age")); System.out.println(String.format("%-10s %10s", "----", "---")); // Now, ask user if he/she wants to save the records to a file for(int i = 0; i < names.size(); i++) { System.out.println(String.format("%-10s %10d", names.get(i), ages.get(i))); } System.out.println(""); // Ask if records must be saved to a file System.out.print("Do you want to save the records to a file? "); String input = askYesNo(sc); if(input.compareTo("yes") == 0) { // ask for file System.out.print("Enter the name of the file: "); String file = sc.nextLine(); // Create Buffered Writer BufferedWriter writer = new BufferedWriter(new FileWriter(file)); // Write lines for(int i = 0; i < names.size(); i++) { writer.write(String.format("%-10s %10d\n", names.get(i), ages.get(i))); } // close file writer.close(); System.out.println("Records successfully saved to " + file + "!"); } // good bye System.out.println("Thanks for playing!"); } public static String askYesNo(Scanner sc) { // This function asks infinitely for user to enter yes/no until he/she enters a valid input String input; while(true) { System.out.print("Enter yes/no: "); input = sc.nextLine().toLowerCase(); // convert to lower case so user can enter Yes, yEs, yeS, YES, etc.. if(input.compareTo("yes") == 0 || input.compareTo("no") == 0) return input; else System.out.println("Invalid input."); } } public static int getAge(Scanner sc) throws Exception { /* This function asks to user for an integer and handles if the user enters an non-numeric value */ while(true) { try { int age; System.out.print("Enter age: "); age = Integer.valueOf(sc.nextLine()); return age; } catch(Exception ex) { System.out.println("Your must enter a valid age."); } } } }