Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I get an error whenever I try to run the following java code: the code should create a file and fill it with the information

I get an error whenever I try to run the following java code:

the code should create a file and fill it with the information given and then read from that file

please make sure that the response is with the full code working since I am new to programing, thanks.

the Account java class:

import java.io.Serializable;

public class Account implements Serializable { private int account; private String firstName; private String lastName; private double balance; // initializes an Account with default values public Account() { this(0, "", "", 0.0); // call other constructor } // initializes an Account with provided values public Account(int account, String firstName, String lastName, double balance) { this.account = account; this.firstName = firstName; this.lastName = lastName; this.balance = balance; }

// set account number public void setAccount(int acct) { this.account = account; }

// get account number public int getAccount() { return account; } // set first name public void setFirstName(String firstName) { this.firstName = firstName; }

// get first name public String getFirstName() { return firstName; } // set last name public void setLastName(String lastName) { this.lastName = lastName; }

// get last name public String getLastName() { return lastName; } // set balance public void setBalance(double balance) { this.balance = balance; }

// get balance public double getBalance() { return balance; } }

-------------------------------------------------------------------------------------

and here is the application code:

import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Scanner; import java.io.EOFException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.NoSuchElementException;

public class ReadWriteSequentialFile { private static FileOutputStream os; private static FileInputStream is; private static ObjectOutputStream out; private static ObjectInputStream in; public static void main(String[] args) throws IOException { Scanner S = new Scanner(System.in); int C; do{ System.out.print("1- Create a file for update(open in Append mode) 2- Read 3- For Exit Choice: "); C= S.nextInt();

switch (C) { case 1: MakeFile(); addRecords(); closeCFile(); break; case 2: InputFile(); readFile(); closeIFile(); break; } }while (C != 3); } public static void MakeFile() { try { os = new FileOutputStream("clients.ser", true); out = new ObjectOutputStream(os); } catch (IOException ioException) { System.err.println("Error opening file."); System.exit(1); } } public static void addRecords() { Scanner S = new Scanner(System.in); System.out.printf("%s%n%s%n%s", " Enter Account number,First Name, Last Name, Balance.","Enter end-of-file indicator to end input.","?");

while ( ! S.hasNext("#")) { try { Account record = new Account (S.nextInt(), S.next(), S.next(), S.nextDouble()); out.writeObject(record); out.writeObject(" "); } catch (NoSuchElementException e) { System.err.println("Invalid object type. Terminating."); S.nextLine(); } catch (IOException e) { System.err.println("Error writing to file. Terminating."); break; } System.out.print("?"); } } public static void closeCFile() { try { os.close(); out.close(); } catch (IOException ioException) { System.err.println("Error closing file. Terminating."); System.exit(1); } } public static void InputFile() { try { is = new FileInputStream("clients.ser"); in = new ObjectInputStream(in); } catch (IOException ioException) { System.err.println("Error opening file."); System.exit(1); } }

public static void readFile() { System.out.printf("%-10s%-12s%-12s%10s%n", " Account", "First Name", "Last Name", "Balance");

try { while (true) { Account record = (Account) in.readObject(); System.out.printf("%-10d%-12s%-12s%10.2f%n", record.getAccount(), record.getFirstName(), record.getLastName(), record.getBalance()); } } catch (EOFException endOfFileException) { System.out.printf("%nNo more records%n"); } catch (ClassNotFoundException classNotFoundException) { System.err.println("Invalid object type. Terminating."); } catch (IOException ioException) { System.err.println("Error reading from file. Terminating."); } }

public static void closeIFile() { try { is.close(); in.close(); } catch (IOException ioException) { System.err.println("Error closing file. Terminating."); System.exit(1); } } }

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

Database Design Using Entity Relationship Diagrams

Authors: Sikha Saha Bagui, Richard Walsh Earp

3rd Edition

103201718X, 978-1032017181

More Books

Students also viewed these Databases questions

Question

Construct the DuPont identity for Smolira Golf Corp.

Answered: 1 week ago

Question

mple 10. Determine d dx S 0 t dt.

Answered: 1 week ago