Answered step by step
Verified Expert Solution
Link Copied!

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.

image text in transcribed

image text in transcribed

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Online Market Research Cost Effective Searching Of The Internet And Online Databases

Authors: John F. Lescher

1st Edition

0201489295, 978-0201489293

More Books

Students also viewed these Databases questions

Question

HOW IS MARKETING CHANGING WITH ARTIFITIAL INTELIGENCE

Answered: 1 week ago

Question

Different types of Grading?

Answered: 1 week ago

Question

Explain the functions of financial management.

Answered: 1 week ago

Question

HOW MANY TOTAL WORLD WAR?

Answered: 1 week ago

Question

=+j Explain the litigation risks in international labor relations.

Answered: 1 week ago

Question

=+j What rules will apply to the process of negotiations?

Answered: 1 week ago