Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***Please add on/modify the existing code below with requirements #3 below*** 3: Create a method (called DataSave) that will write data to a file. This

***Please add on/modify the existing code below with requirements #3 below***

3: Create a method (called DataSave) that will write data to a file. This method will accept an array of Person objects.

The method will open the file (data.txt), and place each person object information (from the array) into the file (1 per line).

Data items you are putting into each line of the file are:

FName (string)

LName (string)

age (int)

married status (boolean).

** You may assume there are appropriate get methods for your use.

***current java code***

import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.InputMismatchException;

import java.util.Scanner;

public class Test {

public static void main(String[] args) throws FileNotFoundException {

int ar[] = new int[10]; // array to contain int value

// scanner to take input Scanner sc = new Scanner(new File("data.txt"));

int input = 0;

for (int i = 0; i < ar.length; i++) { // for length of array

try {

input = sc.nextInt();// reading values from text file line by line

if (input == 0) { // if input is zero

throw new IllegalValueException(); // Throw this exception

}

}

catch (IllegalValueException e) {

// filling array with 1 Arrays.fill(ar, 1);

System.out.println(e);// showing error

break;// breaking out of the loop

}

catch (InputMismatchException e) { // for input other then int

System.out.println("Input value mismatched"); // give warning and continue

Arrays.fill(ar, 0);// filling array with 0

System.out.println(e);// showing error

break;// breaking out of the loop

}

ar[i] = input; // set input to the array // for showing array values System.out.println("Array contains: " + Arrays.toString(ar)); } // for showing array values System.out.println("Array contains: " + Arrays.toString(ar)); sc.close();// closing the file

}

}

class IllegalValueException extends Exception {

IllegalValueException() {

super();

}

}

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 Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions

Question

What would you do about the verbal homophobic and racial insults?

Answered: 1 week ago