This program will read an array of records of address information from a keyboar
ID: 3795268 • Letter: T
Question
This program will read an array of records of address information from a keyboard and store it in a binary file. The binary file is then closed and reopened as an input file to have the information stored back in the array of records and then printed to an output file (with headings).
Use struct to create the record for the following:
The output file contains the following:
C:WINDOWS system32cmd.exe Enter the following information Person's first name: John Person 's last name: Doe Street address 123 Main st city: San Jose State: CA Zip Code: 95122 Enter a Y if you would 1ike to input more data Enter the following information Person s first name: Henry Person"'s last name: Smith Street address :456 Bond Dr City: Sunnyvale State: CA Zip Code: 95188 Enter a Y if you would 1ike to input more data Enter the following information Person's first name: Jack Person s last name: Burger Street address:654 Wall st city: Cupertino State: CA Zip Code: 95014 Enter a Y if you would 1ike to input more data That's all the information Press any key to continue . . .Explanation / Answer
package mypractice;
import java.io.*;
import java.util.*;
/**
* **
* @author ksumanth
*
* This Java function is to read information of Addresses of persons and write to the output File.
*
*/
public class ReadAddresses {
//Declare all the required variables to store the information
public static String firstname;
public static String lastname;
public static String address;
public static String city;
public static String state;
public static String zipcode;
public static String flag = "Y";
//Main Method
public static void main(String[] args) throws IOException{
//FileInputStream is used to read file from a path.
FileInputStream fis = null;
//BufferedReader object is used to read from console
BufferedReader input = null;
//Provide your File input path where the input file is present
input = new BufferedReader(new InputStreamReader(System.in));
//Provide your File output path to write the output
FileWriter writer = new FileWriter("C:\Users\ksumanth\Documents\ReadAddresses\output.txt");
writer.write("firstname lastname address city state zipcode ");
//Asks for the information Repeatedly until the user enters 'N'
while(flag == "Y"){
System.out.println("Enter the following Information :-");
System.out.println("Persons First Name :- ");
firstname = input.readLine();
System.out.println("Persons Last Name :- ");
lastname = input.readLine();
System.out.println("Street Address :- ");
address = input.readLine();
System.out.println("City :- ");
city = input.readLine();
System.out.println("State :- ");
state = input.readLine();
System.out.println("Zip :- ");
zipcode = input.readLine();
//Read the flag variable to find out whether to repeat the loop or not
System.out.println("Enter a Y if you would like to input more data:- ");
flag = input.readLine();
//Write all the information entered to output file
writer.write(firstname+" "+lastname+" "+address+" "+city+" "+state+" "+zipcode);
writer.write(" ");
}
//Final display message
System.out.println("Thats all the Information Press any key to continue ....");
//close the FileWriter object
writer.flush();
writer.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.