Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task 1: Class CreateTextFile uses a Formatter to output formatted Strings, using the same formatting capabilities as method System.out.printf. A Formatter object can output to
Task 1: Class CreateTextFile uses a Formatter to output formatted Strings, using the same formatting capabilities as method System.out.printf. A Formatter object can output to various locations, such as to a command window or to a file. Modify class CreateTextFile so it will output to a different file. Screen shot your program, the command window (which shows user input), and the output of your text file.
import java.io.FileNotFoundException; import java.lang. SecurityException; import java.util.Formatter; import java.util.FormatterClosedException; import java.util.No SuchElementException; import java.util.Scanner; public class Create TextFile { private static Formatter output; public static void main(String[] args) { openFile(); addRecords(); closeFile(); } public static void openFile() { try { output = new Formatter("students.txt"); } catch (SecurityException securityException) { System.err.println("Write permission denied. Terminating"); System.exit(1); // terminate the program } catch (FileNotFoundException fileNotFoundException) { System.err.println("Error opening file. Terminating."); System.exit(1); // terminate the program } } public static void addRecords() { Scanner input = new Scanner(System.in); System.out.printf("%s%n%s%n# ", "Enter ID, first name, last name and marks.", "Enter end-of-file indicator to end input."); while (input.hasNext() // loop until end-of-file indicator { try %s %.2f%n", input.nextInt(), { output.format("%d %s input.next(), input.next(), input.nextDouble(); } catch (FormatterClosedException formatterClosedException) { System.err.println("Error writing to file. Terminating."); break; } catch (NoSuchElementException elementException) { System.err.println("Invalid input. Please try again."); input.nextLine(); // discard input so user can try again } System.out.print("#"); } // end while } public static void closeFile() { if (output != null) output.close(); } import java.io.FileNotFoundException; import java.lang. SecurityException; import java.util.Formatter; import java.util.FormatterClosedException; import java.util.No SuchElementException; import java.util.Scanner; public class Create TextFile { private static Formatter output; public static void main(String[] args) { openFile(); addRecords(); closeFile(); } public static void openFile() { try { output = new Formatter("students.txt"); } catch (SecurityException securityException) { System.err.println("Write permission denied. Terminating"); System.exit(1); // terminate the program } catch (FileNotFoundException fileNotFoundException) { System.err.println("Error opening file. Terminating."); System.exit(1); // terminate the program } } public static void addRecords() { Scanner input = new Scanner(System.in); System.out.printf("%s%n%s%n# ", "Enter ID, first name, last name and marks.", "Enter end-of-file indicator to end input."); while (input.hasNext() // loop until end-of-file indicator { try %s %.2f%n", input.nextInt(), { output.format("%d %s input.next(), input.next(), input.nextDouble(); } catch (FormatterClosedException formatterClosedException) { System.err.println("Error writing to file. Terminating."); break; } catch (NoSuchElementException elementException) { System.err.println("Invalid input. Please try again."); input.nextLine(); // discard input so user can try again } System.out.print("#"); } // end while } public static void closeFile() { if (output != null) output.close(); }Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started