Design a program that prompts the user to create a name and address file from a
ID: 3918425 • Letter: D
Question
Design a program that prompts the user to create a name and address file from a name, street address, city, state, and zip code entered. You should create the name and address file on a drive with a removable disk. Check for output failure in case the user does not have access to the drive (for example, if the disk is not inserted in the drive). If output failure occurs, display a message indicating the problem; otherwise, display a message indicating that the program ended successfully
The following is a copy of the screen results that might display after running your program, depending on the data entered.
---------------------------------------------------------------------------------
Cannot open output file. The program terminates.
--------------------------------------------------------------------------------------
Please enter a name: Judy Shaffer
Please enter a street address: 774 S. Main St.
Please enter a city, state, and zip code: Austin, Texas 78736
Your program ended successfully.
Explanation / Answer
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class FileUse {
public static void main(String[] args) {
String name = new String();
String address = new String();
String other = new String();
Scanner s = new Scanner(System.in);
System.out.println("Please enter a name:");
name = s.nextLine();
System.out.println("Please enter a street address: ");
address = s.nextLine();
System.out.println("Please enter a city, state, and zip code:");
other = s.nextLine();
createFile(name,address,other);
}
public static void createFile(String name, String address, String other){
try{
File file = new File("f://"+name+".txt");
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write("Name: "+ name+" Address: "+address+ " "+ other );
writer.close();
System.out.println("Your program ended successfully.");
}catch(Exception e){
System.out.println(" --------------------------------------------------------------------------------- Cannot open output file. The program terminates. ---------------------------------------------------------------------------------");
}
}
}
//I hope i was able to help u. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.